Log4Net: The Definitive(?) How-To for Those Who Have Already Experienced Some Degree of Frustration Trying to Set It Up the First-Time Around
by Chad Finsterwald
This code sample applies to ASP.NET for the .NET 2.0 framework. Examples are written in C#.

Contents
Problem:
There is no shortage of articles on the web showing how
to set-up Log4Net, but for some reason I still find it to be an overly complicated
affair. I am sure that my frustration says more about me than then about the other authors who
tackled this subject. However, for those few thick-headed (and dashingly handsome) developers
like me, I offer my own step-by-step (frustration free?) guide to Log4Net. As an added bonus, I also include
my Log4Net wrapper, since who wants to be coupled when you can be loosely coupled?
Step 1: Get Log4Net
This is perhaps the most essential step in the process. You can find Log4Net here:
Log4Net Download Page.
Be sure to download the stable release! And now unzip the project to your
preferred unzipping location --no judgement here.
Step 2: Add the Right(!) Log4Net DLL to your project
You will want to click through the following folders until you find the log4net.dll.
(Since the wrapper is for the .NET 2.0 framework, I am assuming you want that one). Here
is the click path starting from the opened unzip Log4Net folder:
bin -> net -> 2.0 -> release -> log4net.dll
Now copy the dll and bring it over to your project. I usually create a "Library" folder
to hold all my referenced Dll's but put it where you want and add a reference to it. (You can
add a reference by right-clicking on your web project and selecting "Add Reference". If it is
still unclear to you, then perhaps the mysteries of Log4Net will have to wait until another day.)
If you did that successfully you should see it under your Bin folder as shown in Image 1.
Step 3: Modify your web.config
Now open your web.config (or add one if you have not already.) You will need to add the
first thing in <configuration> node. (There is an example in the sample
downloadable project.):
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
You will also need to add the following section. This section is the "Appenders", i.e.,
it informs Log4Net how to process the logging request. Should it send to a text file,
SQL Server, an email, etc? The appenders tell it what to do. This is the one clear section
on the Log4Net site. (I can only surmise that the other sections are written by some misanthrope
whose hatered for humanity has so warped their command of the English language as to
cause grevious pain in all who dare read it.) Here is a link to the
appenders config sample section.
And below is the code you should add to your web.config --it too
needs to be added between the <configuration> node.
<log4net>
<root>
<priority value="ALL" />
<appender-ref ref="TraceAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender" />
</root>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="c:\\Log\\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
What these appenders have basically do is cause all logging messages to appear in the Console
output --so you should see all logging messages in the output window when you debug--, to the
Trace diagnostics --so you should see them if you have tracing on--, and to a file --you will
need to ensure that the asp.net user has permission to create and write the log file.
There are a bunch of other appenders included in Log4Net. The two most important others being
the AdoNetAppender and SmtpAppender.
Step 4: Download my Log4Net Wrapper
Download the Log4Net wrapper by clicking
HERE or on the "Download the Code" button at
the top of this page. (Both links download a complete project, just copy
over the AppLog.cs file under the App_Code directory to your App_Code
directory. If you do not have an App_Code directory, add it to your project by right
clicking on your web project, select "Add Asp.Net folder" and chose the "App_Code" folder.)
[Aside: Normally I would not add the AppLog class to my web project, but instead
create a seperate Class Library project which is referenced by my web project. For sake of
clarity, however, we will do everything in the web project.]
Step 5: Add a Global.asax file to your project
Add a global.asax file to your project by right clicking on your web project and
selecting "Add New Item." From the new item dialog, choose the "Global Application Class."
Once this has been added, open it and add the following to the Application_Start method
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AppLog.Init();
AppLog.Write("The Application has started... Yay me!",
AppLog.LogMessageType.Info, this.GetType());
}
If you do not get intellisense when typing AppLog, then it was not correctly added to your project.
If you run the project in Debug Mode, the output window should contain the following --you may need to
scroll to see it.
Step 6: Log Away
I don't want to be too sanguine, but if you followed the steps above or downloaded the
sample application and used it as a guide you should be position to log away to your hearts
content. There is a lot more depth to the Log4Net library then I discussed here, but at
least you have it running and have your sanity. Should you want a part II to this
tutorial exploring the more esoteric depths of Log4Net, let me know in the comments.
Conclusion
As always, if you extend the code or the AppLog wrapper, please send me what you've done.