Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Gain Control of your .NET Logging Using log4net

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.


advertisement
have always considered logging a critical function of any production application; however, it's often overlooked. Logging is often added to the application only after the functionality is already defined and sometimes even after the application has been completely developed.

Many frameworks implement easy and flexible logging functionality in your applications. One of the best-known frameworks for .NET is log4net. This framework is based on the popular "log4j" framework, which was originally developed for Java applications. Log4net is characterized by extremely flexible XML file configuration, a variety of log targets and output formatting options. And of course it comes with full source code.

Starting log4net
I'm not going to go through setup procedure for log4net; instead, I'll just give you some pointers so you'll be able to start logging quickly. For those of you just starting to use the framework, the log4net documentation is excellent.

Here are the basic steps to set up logging in your application:

  1. Create an XML configuration file for the log4net framework to use. This configuration file specifies logger sections, appenders, and the relationships between the two.
  2. Load the configuration file using the following code:
  3. [assembly : log4net.Config.XmlConfigurator( Watch=true)]

  4. Create a log variable that you will use to write log entries.
  5. Use the methods [logvariable].Log(), .Warn(), .Error() or other available methods to write log entries.
XML Configuration files
You can store configuration information in a separate configuration file or as a part of your project's app.config file and is the simplest way to set up log4net logging parameters. It's worth mentioning that anything you can set up via configuration files in log4net can be accomplished through code as well. However, XML configuration files support automatic reloading which can be extremely useful for changing the log configuration while the program is already executing, for example to temporarily enable debug output. For more information about log4net configuration see this section of the log4net documentation.

It's worth mentioning that anything you can set up in log4net via configuration files can be accomplished through code as well.
You must define two main elements in the configuration file: loggers and appenders. Loggers specify classes/namespace hierarchies to be logged, while appenders specify the location to which log4net will log entries. Appenders are destinations that will be used for displaying or storing log entries.

Here's a basic configuration section:

<<configSections>> <<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />> <</configSections>> <<log4net>> <<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >> <<layout type="log4net.Layout.PatternLayout">> <<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />> <</layout>> <</appender>> <<root>> <<level value="INFO" />> <<appender-ref ref="ConsoleAppender" />> <</root>> <<logger name="MyNamespace.MyClass">> <<level value="ALL" />> <<appender-ref ref="CustomAppender" />> <</logger>> <</log4net>>

The system logs entries from the most specific to the most general; in other words, specific loggers will be used when the namespace and class name of the class to be logged are specified explicitly in the configuration. If no match is found, the system applies the root logger and its specified appender, which is the console in the preceding configuration example.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap