Implement Log4Net In It’s Own Config File In 5 Minutes

by Matt 22. December 2009 22:04

I've worked on various projects for a couple of different employers that made use of Log4Net and it was extremely helpful in identifying and resolving some strange behaviours.  However, I'd never had to set it up in a new project myself until recently.  I wasn't sure what to expect when I got into it but it was actually a very painless procedure.

As is customary with these things, you need to download the latest log4net package.  I like to extract the folder structure into a folder called 'lib' that stays under the solution folder for my app. 

In the project you wish to log, add a reference to log4net.dll, located in the folder structure you just extracted. 

Open up your Global.asax.cs file.  This file will contain definitions for many app and session events.  The one you are looking for is called Application_Start.  You will want to add the following line of code to that event.  The event may already be declared (for example if you're in an MVC project), so make sure not to remove any of the existing commands.
you can see a list of all events for your Global.asax here, and a list of the most important events here.

protected void Application_Start()
{
    log4net.Config.XmlConfigurator.Configure();
}

All that is left is to configure log4net to behave the way you wish.  You can put this in your web.config or in a separate file.  I prefer the latter because it keeps my web.config a bit cleaner.

To do this, create a new file at the root of the project, and call it log4net.config.  Open it up and paste the following inside:

<?xml version="1.0"?>
<configuration>
  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="log/log.xml" />
      <appendToFile value="true" />
      <datePattern value="yyyyMMdd" />
      <rollingStyle value="Date" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <locationInfo value="true" />
      </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <root>
      <level value="INFO" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />

    </root>
  </log4net>
</configuration>

 

The above is the configuration that I am using, but you are able to tweak it to suit your needs.  What this essentially does is it creates an appender that writes to a file called 'log.xml' under a folder called 'log'.  NOTE that you will want to change this to a folder that cannot be accessed from the web, as this file will likely store information about your data model and even connection strings.

The above example sets the layout to xml and appends to the existing file, should it already exist, rather than creating a new one each time.  It also creates another appender to output information to the console.

In your class ClassName, you want to declare the following variable:

private static readonly ILog log = LogManager.GetLogger(typeof(ClassName));

And that's it.  Now you can call methods on this variable throughout this application to output all sorts of information.  The basic methods you will use most frequently are as follows, but take a look at everything available on the log object in order to be able to make full use of this great little tool.

log.Info("Info");
log.Warn("Warn");
log.Debug("Debug");
log.Error("Error");
log.Fatal("Fatal");

What I really like about the xml output is that you don't have to monitor an asp.net app as it runs on the server in order to see any problems.  A project such as this one will open up the xml log file created from my configuration above and colour code each entry in the file.  It’s colours will make your eyes bleed, but its a very helpful tool, and the colours can be toned down quite easily.

Tags:
Categories: .NET | ASP.NET | C#