【问题标题】:stacktrace is not being logged by NlogNlog 未记录堆栈跟踪
【发布时间】:2012-09-19 16:18:06
【问题描述】:

我的NLog配置如下

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="file" xsi:type="File" fileName="${basedir}/log${shortdate}.txt" archiveAboveSize="500000" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="10" layout="${date:format=s}|${level}|${callsite}|${identity}|${message}|${exception:format=stacktrace}"/>
      <!--<target name="console" xsi:type="Console" />-->
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" writeTo="file" />
      <!--<logger name="*" minlevel="Debug" writeTo="console" />-->
    </rules>
  </nlog>

但是发生异常时没有记录堆栈跟踪。 NLog 有 bug 吗?

我已经创建了一个围绕 Nlog 的包装器,我的日志记录如下

public void Error(string message, Exception ex)
    {
        logger.Error(message, ex);
    }

我在日志中得到消息,但没有堆栈跟踪。

提前致谢

【问题讨论】:

  • TrustyCoder - 你能把代码贴在你使用 NLog 输出异常的地方吗?你还在 NLog 中得到任何输出吗?

标签: c# logging stack-trace nlog


【解决方案1】:

在你的布局中试试这个:

${exception:format=ToString}

【讨论】:

  • @TrustyCoder:这很令人困惑。我也在使用 NLog 2.0,这对我有用。您确定您调用的是正确的方法重载(即,将异常作为参数的方法)?
【解决方案2】:

您是否尝试过使用 logger.ErrorException(message, ex); 代替 logger.Error?

【讨论】:

  • @SerG 什么是过时的? ErrorException?这在某处有记录吗?
  • @Snixtor 是的。它记录在代码中,属性为[Obsolete("Use Error(String, Exception) method instead.")]
  • @Serg 深入源码,没那么简单。下载 v3.2.0 版本的源代码,Logger.ErrorException 方法标记为已过时。 github.com/NLog/NLog/releases/tag/v3.2.0.0。您引用的属性 在源代码中可见,在 ILogger 接口 github.com/NLog/NLog/blob/master/src/NLog/ILogger.cs 中,但该接口不是 3.2.0 版本的一部分!更进一步,即使 ILogger.ErrorException 在主文件中被标记为过时,Logger.ErrorException 也不是。这绝对是明确的。
猜你喜欢
  • 2014-11-01
  • 2011-06-08
  • 2011-04-06
  • 1970-01-01
  • 2011-06-23
  • 2011-09-16
  • 1970-01-01
  • 2021-02-07
  • 2016-05-14
相关资源
最近更新 更多