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.
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:
XML Configuration files
- Create an XML configuration file for the log4net framework to use. This configuration file specifies logger sections, appenders, and the relationships between the two.
- Load the configuration file using the following code:
[assembly : log4net.Config.XmlConfigurator(
- Create a log variable that you will use to write log entries.
- Use the methods [logvariable].Log(), .Warn(), .Error() or other available methods to write log entries.
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
. 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:
<<conversionPattern value="%date [%thread] %-5level %logger
[%ndc] - %message%newline" />>
<<level value="INFO" />>
<<appender-ref ref="ConsoleAppender" />>
<<level value="ALL" />>
<<appender-ref ref="CustomAppender" />>
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.