Get the actual filename for target

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Get the actual filename for target

z4711
you need to write a custom target

follow the instructions under nlog-project.org/wiki/How_to_write_a_Target

then modify it to be an interceptor only and add the ActualLogfile property

[Target("Interceptor")]
public sealed class InterceptTarget : FileTarget
{
    public string ActualLogfile { get; set; }

    protected override void Write(LogEventInfo logEvent)
    {
        ActualLogfile = this.FileName.Render(logEvent);
        string logMessage = this.Layout.Render(logEvent);
        base.Write(logEvent);
    }
}
NLog.config should be something like this:

<nlog>
  <targets>
    <target name="i"
            xsi:type="Interceptor"
            fileName="${basedir}\logs\${shortdate}.log" />
  </targets>
  <rules>
    <logger name="*" minLevel="Trace" writeTo="i"/>
  </rules>
</nlog>
remark: be shure to generate a very early log message (at pgm start), before ActualLogFile is empty.