【问题标题】:Nlog ${callsite-linenumber} not working properly with async methodsNlog ${callsite-linenumber} 不适用于异步方法
【发布时间】:2017-11-03 08:44:00
【问题描述】:

我正在使用 NLog v4.4.12。

我有一个异步方法,我必须在其中记录包含行号的信息。问题是,如果我在调用 async 方法之前记录任何内容,则 line lumber 始终为 0。但是,如果我在调用 async 方法之后记录任何内容,则行号始终会正确打印。其他人有这个问题吗?

例子:

public class TestController : ApiController {
    private static HttpClient Client = new HttpClient(new HttpClientHandler { Proxy = null, UseProxy = false })
                                                                            { Timeout = TimeSpan.FromSeconds(double.Parse(WebConfigurationManager.AppSettings["httpTimeout"])) };
    private static readonly Logger logger = LogManager.GetCurrentClassLogger();

    public async Task DoSomething(int clientId)
    {
        logger.Info($"Here, line number is always 0");

        try
        {
            var clientBooks = new List<string>();
            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            using (var response = await Client.GetAsync(new Uri(WebConfigurationManager.AppSettings["cb"] + "books?clientId=" + clientId)))
            {
                if (response.IsSuccessStatusCode)
                {
                    clientBooks = await response.Content.ReadAsAsync<List<string>>();
                }
                else
                {
                    throw new Exception($"Error getting books");
                }
            }

            logger.Info($"Here, line number always prints correctly");

            // Here I have some other logic ...
        }
        catch (Exception ex)
        {
            logger.Error(ex);
        }

        logger.Info($"Here, line number always prints correctly");
    }
}

我的 NLog 配置:

<nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <variable name="logDirectory" value="${basedir}/Logs/${shortdate}" />
    <variable name="format" value="${longdate}|${activityid}|${logger}|${callsite-linenumber}|${level:uppercase=true}|${message}" />
    <targets>
      <target name="fileLogErrors" xsi:type="File" fileName="${logDirectory}/errors.txt" layout="${format}" encoding="utf-8" />
      <target name="fileLog" xsi:type="File" fileName="${logDirectory}/all.txt" layout="${format}" encoding="utf-8" />
    </targets>
    <rules>
      <logger name="*" minlevel="Warn" writeTo="fileLogErrors" />
      <logger name="*" minlevel="Info" writeTo="fileLog" />
    </rules>
 </nlog>

【问题讨论】:

  • 我不确定在使用 async 时信息是否可用(在 .NET 内部) - 您可以尝试打印完整的堆栈跟踪吗? ({${stracktrace})。
  • @Julian 这是堆栈跟踪(仅包括 ${stacktrace}|Line:${callsite-linenumber}):AsyncTaskMethodBuilder.Start => b__0>d.MoveNext => AttributesBaseController。 UpdateEquationAccountsInfo|Line:0 ExecutionContext.RunInternal => MoveNextRunner.InvokeMoveNext => d__9.MoveNext|Line:457
  • 这是一个错误/缺失的功能,请在此处发布问题:github.com/NLog/NLog/issues/new
  • @Julian 我在 GitHub 上打开了这个问题。 link

标签: nlog


【解决方案1】:

NLog 版本。 4.5解决了这个问题:https://github.com/NLog/NLog/issues/2382

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2017-08-07
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多