Jul 9, 2010

Log4net

This logging infrastructure can sometimes be frustrating to setup, especially with the RollingFileAppender who wouldn't create and write to that damn log file specified in the app.config

So, one thing that helps, in the app.config of the app, put the log4net in debug like this:
<log4net debug="true">

This way, errors in log4net will be sent to the Output (Ctrl+W, O) -> Show output from: Debug.
Here are 2 ways to configure before sending stuff to the log.

1. Call Configure() somewhere in the app (like Program.cs):
log4net.Config.DOMConfigurator.Configure();

But that means that you should modify the config section in the app.config:
<configSections>
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

OR

2. You can add in AssemblyInfo.cs:
[assembly: log4net.Config.DOMConfigurator(Watch = true)]

And then modify in the app.config sections:
<configSections>
   <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>


Here is the rest of the log4net config in the app.config:

<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="TestLog4net.log" />
      <param name="AppendToFile" value="true" />
      <param name="ImmediateFlush" value="true" />
      <param name="MaxSizeRollBackups" value="10" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[--- Log started ---]\r\n" />
        <param name="Footer" value="[--- Log ended ---]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>

Here is some C# to demo this log4net logging infrastructure:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;

namespace TestLog4Net
{
    class Program
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            // Using the first method to setup log4net
            DOMConfigurator.Configure();

            _log.Debug("Here is a debug log.");
            _log.Info("... and an Info log.");
            _log.Warn("... and a warning.");
            _log.Error("... and an error.");
            _log.Fatal("... and a fatal error.");
           
            Console.Read();
        }
    }
}

Here is the output in TestLog4net.log:

[--- Log started ---]
2010-07-09 12:44:40,072 [3996] DEBUG TestLog4Net.Program [] - Here is a debug log.
2010-07-09 12:44:40,088 [3996] INFO TestLog4Net.Program [] - ... and an Info log.
2010-07-09 12:44:40,088 [3996] WARN TestLog4Net.Program [] - ... and a warning.
2010-07-09 12:44:40,088 [3996] ERROR TestLog4Net.Program [] - ... and an error.
2010-07-09 12:44:40,088 [3996] FATAL TestLog4Net.Program [] - ... and a fatal error.
[--- Log ended ---]


Now, here are 2 resources providing more detail:
Log4Net Tutorial
A Brief Introduction to the log4net logging library, using C#