NLog内部异常登录

时间:2017-05-31 14:28:21

标签: logging nlog exception-logging

我尝试使用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 文件的See the inner exception for details行和Exception Message行收到相同的消息InnerException Message

2 个答案:

答案 0 :(得分:6)

这对我有用:

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

答案 1 :(得分:1)

另一种可能性是在内部异常之间添加新行是在xml中使用linebreak count,因为&#xD;&#xA没有以某种特殊方式解析并且在写入时被加载。所以innerExceptionSeparator将不起作用。

innerExceptionSeparator=${newline}

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

但是只有在使用layout="${message}${onexception:${newline}${exception:maxInnerExceptionLevel=10:innerExceptionSeparator=&#xD;&#xA;&#x9;:format=shortType,message}} 方法时才有效。 ILogger.Error(Exception, string)忽略此布局