Configuring NLog to log exceptions in an XML output?

被刻印的时光 ゝ 提交于 2019-12-05 02:36:42

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}" />
      </layout>
</target>

Would write:

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

See docs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!