log4net on the Compact Framework

I was reminded of log4net today when working on a project. The client asked a logging framework and the cool part is that it’s a Compact Framework application. Sure enough log4net supports the Compact Framework.

There are a couple of minor differences that you should be aware of if you are interested in using log4net on the Compact Framework. First of all, not all of the appenders make sense – like the RemotingAppender, the EventLogAppender and the OracleAppender. Secondly, there is no mechanism for retrieving assembly level attributes so you have to explicitly initialize and shut down the log4net engine.

 

static void Main()

{

      try

      {

            //This line configures the log4net engine.

log4net.Config.DOMConfigurator.Configure(

new FileInfo(@”\log4net.config”));

 

            frmLogin loginForm = new frmLogin ();

            loginForm.ShowDialog();

                  Application.Run(new frmMain());

            }

      }

      catch (System.Exception e)

      {

            _log.Error(“Main“, e);

      }

      finally

      {

            //This line shuts down the log4net engine

            log4net.LogManager.Shutdown();

      }

}

 

The other thing that was interesting is that the online help on http://logging.apache.org/log4net showed conversion patterns with %message%newline to print the message and then put a newline in the file. I had to use %m%n because the first one there printed as

 

My stringessage

ewline

 

which was not quite what was expected.

 

Have fun with log4net, it’s a great lightweight logging framework for the compact framework.

Leave a Reply

Your email address will not be published. Required fields are marked *