Tuesday, August 15, 2006

log4net setup for .NET 2.0

For some reason, even though setting up log4net is relatively simple, I always spend too much time trying to configure it for various types of applications (Web, Console, Web Service, Windows Service, Windows App), and after I figure it out, I forget to document what I did to get it working. Here's an attempt to do this.

Console Apps:
First, you need to download log4net. You can get it from the log4net Downloads page.

Once you've installed it, add a reference to it from your project that will perform the logging. The location of the log4net.dll for 2.0 on my machine is at C:\log4net\log4net-1.2.10\bin\net\2.0\release

Add a section in the configSections area of the hosting applications' app.config or web.config file:


<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>


Add the log4net configuration settings to the app.config:


<log4net>
<!-- RollingFileAppender looks after rolling over files by size or date -->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="c:\\Logs\\RollingLogHelloWorld.log">
<param name="AppendToFile" value="true">
<param name="MaxSizeRollBackups" value="10">
<param name="MaximumFileSize" value="1000">
<param name="RollingStyle" value="Size">
<param name="StaticLogFileName" value="true">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %-45c [%x] - %m%n">
</layout>
</appender>
<!-- FileAppender appends to a log and it is manually managed or size -->
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="c:\\Logs\\LogHelloWorld.log">
<!-- Example using environment variables in params -->
<!-- <param name="File" value="${TMP}\\ApplicationKit.log"> -->
<param name="AppendToFile" value="true">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n">
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO">
<appender-ref ref="FileAppender"></appender-ref>
</level>
</root>
</log4net>


This configures log4net to use one or more appenders, either a RollingFileAppender or a FileAppender. The <root> element defines which file appender it will use.

The log files are created in the c:\Logs directory. Make sure that the process that hosts the classes that perform the logging (classes contained either within the exe or within a class library dll) has write access to the directory where files are created. For windows and console applications the user identity is the current logged in user that executes the console application. For windows services, the user identity is the user that the service is configured to run as. For ASP.NET applications, give the ASPNET user access Full control rights to the webroot folder (the physical folder location for the virtual directory of the app) for the application. This will allow the aspnet_wp.exe process to create/write/update the log file

You can also add a switch that will output log4net's debug information to the Visual Studio output debug window as an appSetting:


<appsettings>
<add key="log4net.Internal.Debug" value="true">
</add>
</appsettings>


If you have an AssemblyInfo.cs file in your project, you should add an entry to it which will configure the logging framework:


[assembly: log4net.Config.DOMConfigurator()]


Alternatively, you can put the initialization function into your code, you should execute the following line when the application first starts:

log4net.Config.XmlConfigurator.Configure();

In each class that will perform logging, add the following using statements:

using log4net;
using log4net.Config;

Add the following static member variable to each class, replacing <class name=""> with the name of the class:


private static readonly ILog log = LogManager.GetLogger(typeof (<class name="">));


when you want to log something, you can try this:


log.Info("Hello World.");

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?