tags:

views:

87

answers:

1

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="file" />
</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.

+1  A: 

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.

liquidrogue