${Callsite} filename doens't work with async #1813

Open
304NotModified opened this Issue Dec 4, 2016 · 1 comment

Projects

None yet

1 participant

@304NotModified
Member
304NotModified commented Dec 4, 2016 edited

@valuerr wrote:

Got same bug with callsite:fileName=True on Nlog versions 4.3.11 (stable) and 5.0.0-beta03

The code:

using System.Threading;
using System.Threading.Tasks;
using NLog;

namespace ConsoleApplicationPR
{
    class Program
    {
        private static Logger Log;

        private static async Task DoSomeWorkAsync()
        {
            Log.Debug("DoSomeWorkAsync:start");
            await Task.Delay(100);
            Log.Debug("DoSomeWorkAsync:exit");
        }

        static void Main(string[] args)
        {
            Log = LogManager.GetCurrentClassLogger();
            Log.Debug("Main:start");
            DoSomeWorkAsync();
            Log.Debug("Main:exit");
            Thread.Sleep(3000);
        }
    }
}

outputs:

d:/develop/workspaces/dotnet/ConsoleApplicationPR/ConsoleApplicationPR/bin/Debug/ConsoleApplicationPR.exe
02:23:38.8021 [DEBUG] [thread 1] (Program.cs:21) Main:start
02:23:38.8306 [DEBUG] [thread 1]  DoSomeWorkAsync:start
02:23:38.8306 [DEBUG] [thread 1] (Program.cs:23) Main:exit
02:23:38.9392 [DEBUG] [thread 5] (Program.cs:15) DoSomeWorkAsync:exit

simplified App.config:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
        autoReload="true"
        throwExceptions="false"
        internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

    <variable name="callsiteFormat"
              value="${callsite:className=False:fileName=True:includeSourcePath=False:methodName=False:cleanNamesOfAnonymousDelegates=False:skipFrames=0}" />
    <!--    Not works with async methods; -->
    <!--    Todo: commit issue-->

    <variable name="NormalLayout"
              value="${time} [${level:uppercase=true}] [thread ${threadid}] ${callsiteFormat} ${message}" />

    <targets>
      <target xsi:type="ColoredConsole"
              name="console"
              layout="${NormalLayout}" />
    </targets>

    <rules>
      <logger name="*" minlevel="Trace" writeTo="console" />
    </rules>
  </nlog>
</configuration>

P.S. The bug is actual on msvc 2015, 14.0.25431.01 update 3, .NET 4.6.01586

Didn't test it on Mono.

@304NotModified 304NotModified added the bug label Dec 4, 2016
@304NotModified
Member

@valuerr thanks for the test case!

merged into feature branch fix-callsite-async-1812

This is a bit difficult to fix, as .NET gives us "unkown" for this stackframe:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment