开发者

Configuring NLog to log exceptions in an XML output?

Currently, we have NLog spitting out CSV files just to prove we have NLog actually logging exceptions.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogToConsole="true" internalLogToConsoleError="true">
<targets>
  <target name="file" xsi:type="File"  fileName="${specialfolder:folder=ApplicationData}/log.csv">
    <layout xsi:type="CSVLayout">
      <column name="User_Machine_Name" layout="${machinename}" />
      <column name="Time" layout="${date}" />
      <column name="Level" layout="${level}" />
      <column name="Message" layout="${message}" />
      <column name="Exception_Message" layout="${exception:format=Message}"/>
      <column name="Exception_Type" layout="${exception:format=Type}"/>
      <column name="Callsite_Class" layout="${callsite:methodName=false}" />
      <column name="Callsite_Method" layout="${callsite:className=false}" />
      <column name="Stack_Trace" layout="${stacktrace:format=DetailedFlat}"/>
    </layout>
  </target>
  <target name="console" xsi:type="Console"
    layout="${longdate}|${level}|${message}">
  </target>
</targets>
<rules>
  <logger name="*" minlevel="Trace" writeTo="fil开发者_JS百科e" />
</rules>

This is working as expected except that I need it to output in XML. I've looked through NLog documentation and the only thing I've found is that there's a Log4JXmlEventLayout but the documentation doesn't go into how to use it. I'm new to NLog and I can't find too many resources on the subject.


As far as I can tell, the Log4JXmlEventLayout has some properties associated with it (stack trace-ish information, calling class, time, etc), but that's about it. I've looked into how to include additional information, but it seems as if that's not possible.

A possible configuration looks like this:

<target name ="xmlFile" xsi:type="File"
                fileName="${tempdir}/${processname}/log.xml"
                archiveFileName="${tempdir}/${processname}/archive/log_{#####}.xml"
                archiveAboveSize="10000000"
                layout="${log4jxmlevent:includeSourceInfo=true:includeCallSite=true:includeMDC=true:appInfo=true:includeNDC=true:includeNLogData=true}"/>

However, I've found that only NLog 2.0 will actually make use of attributes like "includeSourceInfo". It appeared to me that in NLog 1.0, when these were set to true, the resulting xml contained only the date, level, and message.

One reason not to use Log4JXmlEventLayout is that it doesn't do anything with exceptions, i.e. if you call

logger.ErrorException("This shouldn't happen", exception);

the logger will write down "This shouldn't happen", but the exception info is gone.

Maybe it would be possible to create a custom xml wrapper around the data you want. I haven't done so, but I'm thinking about it simply due to the limitations around the Log4JXmlEventLayout.


Since NLog 4.6 there is a XML layout. With that layout you could define your own XML. E.g.

<target name="xmlFile" xsi:type="File" fileName="${logFileNamePrefix}.xml" >
      <layout xsi:type="XmlLayout" includeAllProperties="false" elementName='logevent'>
              <attribute name="time" layout="${longdate}" />
              <attribute name="level" layout="${level:upperCase=true}"/>
              <element name="message" value="${message}" />
              <element name="exception_type" layout="${exception:format=Type}"/>
      </layout>
</target>

Would write:

<logevent time="2010-01-01 12:34:56.0000" level="ERROR">
   <message>hello, world</message>
   <exception_type>System.ArgumentNullException</exception_type>
</logevent>

See docs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜