RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Gain Control of your .NET Logging Using log4net : Page 2

Don't build logging capabilities for your applications from scratch; you can get robust and flexible logging functionality for your .NET applications with the free open source log4net framework, and then extend it to support custom needs.

Initializing and Using Log4net
You need only two simple steps to initialize and use log4net:

  1. Add an assembly-level attribute to the executing assembly as shown below:
  2.    [assembly : log4net.Config.XmlConfigurator(Watch=true)]
    The preceding attribute causes the log4net framework to load the XML configuration from the app.config file and monitor the file for any configuration changes. You can also use the XmlConfigurator class to load and watch configuration files by calling the Configure() and ConfigureAndWatch() methods directly.

  3. Next, create a static variable that you'll use to call log4net framework, and use the default LogManager to initialize it to the default Logger implementation
       private static readonly ILog log = LogManager.GetLogger(
You should declare a static variable in each class for logging information; you should not share the variable across classes. However, by bending that rule and knowing that logger properties are initialized at the point of variable declaration, it is possible to create multiple logger instances initialized to different values.

To send a string for logging, use the Debug(), Info(), Error() or other methods available through your declared variable from the ILog interface.

You should declare a static variable in each class for logging information; you should not share the variable across classes.
Custom Appenders: Adding Destinations
One common customization is creating a custom appender that logs messages to a proprietary destination, such as an MSMQ queue, a database table, etc. Log4net makes it extremely easy to create custom appenders. A custom appender class must implement the IAppender interface. The easiest (and recommended) way is to create a class that inherits from the log4net.Appender.AppenderSkeleton class, and implement the required function void Append(LoggingEvent loggingEvent). What you do within the loggingEvent method is entirely up to you, based on the needs of your custom appender. For example, here's a custom appender that simply shows a MessageBox:

   protected override void Append(LoggingEvent loggingEvent)
A couple of things you should remember:

  • Appenders are called in the order in which you define them in the configuration file.
  • Calls to appenders are synchronous and therefore you should optimize custom appenders for speed.
The log4net framework loads appenders using reflection because that makes it possible to reference classes that exist in both the executing assembly and in different assemblies by specifying full assembly reference in the <appender> configuration element.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date