Search code examples
xmllog4netlog4net-configuration

Log4Net XmlLayout Custom Properties


I have Log4Net set up to to output xml, and I wanted to see if anyone knew how to add custom properties that will output as seperate nodes in the xml.

Here's the current log4net config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <file type="log4net.Util.PatternString" value="C:\Log.xml"/>
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maximumFileSize value="10MB" />
      <maxSizeRollBackups value="5000" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <locationInfo value="true"/>
      </layout>
      <param name="Encoding" value="utf-8" />
      <filter type="log4net.Filter.PropertyFilter">
        <Key value="Version" />
        <StringToMatch value="1" />
      </filter>
    </appender>
  </log4net>
</configuration>

I've seen a post on doing this when writing to SQL, but nothing on xml writing, and I can't seem to bridge the gap there. Obviously the goal is to have properties like UserID, ClientID, etc, as seperate xml nodes so we have better filtering control in our Log4Net Viewer (using YALV)


Solution

  • You do not have control over the log4net.Layout.XmlLayoutSchemaLog4j, because there is not customizing implemented in the layout. However you can implement your own layout by following the answer on this question: Log4net xml output