How can I query the path to an NLog log file?
10,212
Solution 1
I've just tried to get this information via the configuration api.
Sadly it looks like the configuration is evaluated by the actual target and is not resolved in the configuration.
As {basedir}
refers to the appdomain base directory you could simply read this value on your own.
var basedirPath = AppDomain.CurrentDomain.BaseDirectory;
Solution 2
private string GetLogFile()
{
var fileTarget = LogManager.Configuration.AllTargets.FirstOrDefault(t => t is FileTarget) as FileTarget;
return fileTarget == null ? string.Empty : fileTarget.FileName.Render(new LogEventInfo { Level = LogLevel.Info });
}
Author by
aknuds1
Pragmatic Go/Rust/JavaScript ++ software engineer Homepage GitLab LinkedIn profile
Updated on June 09, 2022Comments
-
aknuds1 almost 2 years
I've configured a file target for NLog as follows:
<targets> <target name="asyncFile" xsi:type="AsyncWrapper"> <target xsi:type="File" name="logfile" fileName="${basedir}/Logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> </target> </targets>
How can I query the actual filesystem path (
fileName
) of thisFile
target via NLog's API? -
Kevin Marshall almost 5 yearsWorked like a charm!