Creating your own Pattern Layout Converter for Log4net

Pattern Layout Converter is the way you tell log4net how to log something that it doesn’t know yet.

You first create your class that will get the information, for instance, this class will is to log the machine name:

using System;
using System.IO;
using log4net.Layout.Pattern;

namespace MyApplication.Logging
{
    public class MachinePatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            writer.Write(Environment.MachineName);            
        }
    }
}

Then, you edit your config file adding the converter you just created:

  <log4net>
    <appender name="Application" type="log4net.Appender.FileAppender">
      <file value="log\application.log" />
      <appendToFile value="true" />
      <maximumFileSize value="1024KB" />
      <layout type="log4net.Layout.PatternLayout">        
        <converter>
          <name value="machine" />
          <type value="MyApplication.Logging..MachinePatternConverter" />
        </converter>        
        <conversionPattern value="%date [%thread] %level %logger %machine - %message e:%exception%newline %newline" />
      </layout>
    </appender>
<!-- lines removed for brevity -->

In the code above, we added our new converter and named it “machine”. Then, In the conversionPattern line, we can use it wherever we want by adding the %machine variable.

Here are others converters you may want to use:

    public class IPPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (HttpContext.Current != null)
            {
                writer.Write(HttpContext.Current.Request.UserHostAddress);
            }
        }
    }
    public class UrlPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (HttpContext.Current != null)            
                writer.Write(HttpContext.Current.Request.Url.AbsoluteUri);
        }
    }
    public class UserAgentPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (HttpContext.Current != null)
            {
                writer.Write(HttpContext.Current.Request.UserAgent);
            }
        }
    }

Now, applying all converter in our config file:

  <log4net>
    <appender name="Application" type="log4net.Appender.FileAppender">
      <file value="log\application.log" />
      <appendToFile value="true" />
      <maximumFileSize value="1024KB" />
      <layout type="log4net.Layout.PatternLayout">
        <converter>
          <name value="url" />
          <type value="MyApplication.Logging.UrlPatternConverter" />
        </converter>
        <converter>
          <name value="ip" />
          <type value="MyApplication.Logging.IPPatternConverter" />
        </converter>
        <converter>
          <name value="machine" />
          <type value="MyApplication.Logging.MachinePatternConverter" />
        </converter>
        <converter>
          <name value="userAgent" />
          <type value="MyApplication.Logging.UserAgentPatternConverter" />
        </converter>
        <conversionPattern value="%date [%thread] %level %logger %url %ip %machine %userAgent - %message e:%exception%newline %newline" />
      </layout>
    </appender>

Do you have any interest Pattern Layout Converter you use in your application?

Advertisements

7 thoughts on “Creating your own Pattern Layout Converter for Log4net

  1. Awesome. Thanks for the example. Implemented without any problems. I had seen other solutions that were not nearly as elegant. Yours is clean, easy and elegant.

    1. It was the snow falling effect that wordpress gives you but I disabled it since it’s broken for this theme.
      Thank you for your feedback!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s