Working with Log4net - Getting Started
(Page 2 of 4 )
Setting up your project to use log4net is straightforward. Just download the distribution from the Downloads link on the tool’s home page, extract the log4net distribution, and follow these four steps.
Step 1: Referencing the assembly
You must select the correct build of log4net for your environment. For example, if you are using version 1.1 of the .NET Framework, you should select the log4net.dll file located in the bin\net\1.1\release folder in the log4net distribution. To add a reference to your Visual Studio project, select Add Reference from the Project menu.
Step 2: Configuring log4net
log4net can be configured in the app.config file or via an external XML file. To configure log4net in the app.config file, add the following fragment to the file under the configuration element:
<configSections>
<section name="log4net"
type="log4net.Config. Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="MyAppender1" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout
value="%date [%thread]
%-5level %logger - %message%newline" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="MyAppender1" />
</root>
</log4net>
To configure log4net in an external XML file, or to review configuration options and examples, please see the log4net web site.
Step 3: Initializing logging within the application
There are two ways to initialize logging within your application. The first is to directly configure log4net in your code; this call should be made as early as possible during application initialization. Use a code fragment like the following that is appropriate for your language:
log4net.Config.XmlConfigurator.Configure();
There are a number of overloaded versions of this method that you can use to supply parameters such as the name of a configuration file. Further details are available in the log4net documentation.
Alternatively, you can create an attribute on the application’s main assembly specifying the location of the log4net configuration settings. log4net will then automatically configure itself when the first logger is created. The assembly attribute should be specified in a project source file, such as AssemblyInfo.cs or AssemblyInfo.vb, as follows:
[assembly: log4net.Config.XmlConfigurator()]
Step 4: Adding logging messages to the code
To output logging messages from code, you must first create a logger. You can do this by adding the following line of code to each of your types (in this example, we assume that the type is named Form1):
private static readonly ILog log = LogManager.GetLogger(typeof(Form1));
Logging messages can then be added as follows:
// Simple example
log.Debug("Something happened");
// Example showing variable substitution
log.DebugFormat("Something happened because {0} is less then {1}", var1, var2);
// More complex example demonstrating level test
if (log.IsDebugEnabled)
{
string s = ComplicatedMethod();
log.DebugFormat("The value of ComplicatedMethod is {0}", s);
}
Next: Using log4net >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly, 2006; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|