【问题标题】:NLog Inner Exception LoggingNLog 内部异常记录
【发布时间】:2017-11-01 10:03:32
【问题描述】:

我正在尝试使用 NLog 记录内部异常消息。这是我的 NLog.config 文件的一部分:

    <target name="errors" xsi:type="File" layout="${longdate}${newline}
        - Exception Message: ${exception:format=Message}${newline}
        - InnerException Message: ${exception:innerExceptionSeparator=TEXT}${newline}"
        fileName="\Logs\errors-${shortdate}.log"
       concurrentWrites="true" />
    </targets>

对于 NLog.config 文件的 Exception MessageInnerException Message 行,我收到了相同的消息 See the inner exception for details

【问题讨论】:

    标签: logging nlog exception-logging


    【解决方案1】:

    这对我有用:

      <target name="errors" xsi:type="File" layout="
                ${message}
                ${onexception:EXCEPTION OCCURRED\:
                ${exception:format=type,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}"
                fileName="\Logs\errors-${shortdate}.log"
                concurrentWrites="true"
                />
      </targets>
    

    属性描述:

    • ${exception:maxInnerExceptionLevel=N} - 控制记录多少内部异常。为了向后兼容,默认为零。
    • ${exception:innerExceptionSeparator=TEXT} - 定义分隔内部异常的文本。默认为新行字符串(特定于平台)。
    • ${exception:innerFormat=FORMATSTRING} - 定义内部异常的格式与${exception:format=FORMATSTRING} 定义顶级异常的格式相同。如果未指定此参数,则顶级异常和内部异常使用相同的格式。

    这是用于记录内部异常的官方Nlog Documentation

    【讨论】:

    • 多行布局导致日志消息不是从控制台窗口的最左侧位置开始,对于文件也是如此。 ${message} 和行开始之间的空格(制表符)显示在日志中
    【解决方案2】:

    另一种可能性是在内部异常之间添加新行是在 xml 中使用换行符&amp;#xD;&amp;#xA,因为innerExceptionSeparator 没有以某种特殊方式解析,而是在写入时加载。所以innerExceptionSeparator=${newline} 将不起作用。

    layout="${message}${onexception:${newline}${exception:maxInnerExceptionLevel=10:innerExceptionSeparator=&#xD;&#xA;&#x9;:format=shortType,message}} 
    

    这将导致内部异常文本从新行开始并由制表符缩进

    但是,这仅在使用 ILogger.Error(Exception, string) 方法时才有效。 ILogger.Error(Exception) 忽略此布局

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 2014-10-04
      • 2017-11-22
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多