Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query the path to an NLog log file?

Tags:

.net

nlog

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 this File target via NLog's API?

like image 291
aknuds1 Avatar asked Sep 07 '11 10:09

aknuds1


2 Answers

    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 });
    }
like image 108
ZOS Avatar answered Oct 26 '22 17:10

ZOS


I've just tried to get this information via the configuration api.

enter image description here

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;
like image 33
ccellar Avatar answered Oct 26 '22 19:10

ccellar