10

我正在尝试使用 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>

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

4

2 回答 2

18

这对我有用:

  <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 文档

于 2017-05-31T14:40:22.450 回答
5

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

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

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

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

于 2017-10-04T09:52:47.213 回答