preload
Oct 10

apacheWhen developing all kind of projects a good strategy for logging can save you a lot of time and frustration. Personally I am very found of using Apache log4net and this post will describe the most important features in log4net and how to implement and use this in a .Net Compact Framework project.

log4net can be configured with different appenders that logs to different sources (file, output window, smtp, server ++). You can also configure what kind of log levels will be written to the different appenders (info, debug, warn, error and fatal).

Most of the time I use two different appenders: I use the DebugAppender to log to my output window in Visual Studio, I like to set this appender to log all levels. I also like to use a LogFileAppender to log to a file on the device, I usually set this appender to only log error and fatal.

You can read documentation and download Apache log4net at http://logging.apache.org/log4net/index.html

I will show you a simple example on how to use log4net in a .Net Compact Framework project:

First you create a new Smart Device project and chose Device application. I am using Visual Studio 2008, Windows Mobile 6 professional SDK as target platform and .Net Compact Framework 3.5

The first thing you need to do after creating the project is to add the log4net.dll as a reference (log4net.dll can be downloaded from the link above).

Then you need to add a xml configuration file for log4net in your project, I added a file called Config.xml in my project and set the property to content/copy always. In this configuration file you add and setup all the different appenders and what level they will log on. You can also defined a layout pattern for the log text.

Below you can see my very simple Config.xml file containing a LogFileAppender and a DebugAppender and they are both set to log all levels.

  1. <!– .NET application configuration file –>
  2. <configuration>
  3.  <!– This section contains the log4net configuration settings –>
  4.  <log4net>
  5.   <!– Define some output appenders –>
  6.   <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
  7.    <file value="log-file.txt" />
  8.    <appendToFile value="true" />
  9.    <layout type="log4net.Layout.PatternLayout">
  10.      <conversionPattern value="%date [%-5level] – %message%newline" />
  11.    </layout>
  12.   </appender>
  13.     <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
  14.       <layout type="log4net.Layout.PatternLayout">
  15.         <conversionPattern value="%date [%-5level] – %message%newline" />
  16.       </layout>
  17.     </appender>
  18.     <!– Setup the root category, add the appenders
  19.            and set the default level –>
  20.   <root>
  21.    <level value="ALL" />
  22.       <appender-ref ref="DebugAppender" />
  23.       <appender-ref ref="LogFileAppender" />
  24.   </root>
  25.  </log4net>
  26. </configuration>

In your Main method (by default found in Program.cs) you need to configure log4net based on the Config.xml file

//Load Config.xml to setup log4net
  1. string path = System.IO.Path.GetDirectoryName(
  2.     System.Reflection.Assembly.GetExecutingAssembly()
  3.    .GetModules()[0].FullyQualifiedName)
  4.    + "\\Config.xml";
  5. if (System.IO.File.Exists(path))
  6.   {
  7.       XmlConfigurator.Configure(new System.IO.FileInfo(path));
  8.   }

log4net are now ready to be used in all classes throughout your project. For each class you want to use log4net create a static logger object in top of your class. Remember that the typeof () must be set to the class you create this logger in.

// Create a logger for use in this class
  1. private static readonly log4net.ILog Log =
  2.              log4net.LogManager.GetLogger(typeof (Program));

I often use the logger to log exceptions but I also use it for information and debug.

//Information logging
  1. Log.Info("Application startup");
  2. //Debug logging
  3. Log.Debug("Debug statement");
  4. //Warning logging
  5. Log.Warn("Could not find all images");
  6.  
  7. //Exception logging
  8. catch(Exception ex)
  9.   {
  10.      Log.Error("Custom exception message", ex);
  11.   }

The log file on the device can be found under program files\yourProjectName\log-file.txt. This text file on the device is a very good place to for example log your global exception handling.

Share & enjoy
You can subscribe to my comments feed to keep track of new comments.

2 Comments to “Using Apache log4net in .Net Compact Framework projects”

  1. Vijay says:

    Log4Net Configuration Loading has to be corrected to

    log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path));

  2. I have added “using log4net.Config;” at the top of my class so I can access XmlConfigurator directly without prefixing it with the namespace.
    I forgot to mention that in the code example.
    Thanx :)

No Pingbacks to “Using Apache log4net in .Net Compact Framework projects”

Leave a Reply

Subscribe to my comments feed

Subscribe to my feeds Follow me on Twitter
Microsoft MVP