Search This Blog

Log4net in a .NET 2 web application

posted on Monday, April 9, 2012

Before I begin with this unbelievably interesting blog post, I'll start by apologizing for taking so long to post, I've been a bit too busy.

So, back to business! Serious business, because this post is about logging! Personally, I think logging can be a very useful tool for troubleshooting an application. But unfortunately I haven’t always seen this as a priority to implement. The reason why is mostly because I used to think it would take too much time to implement and there’s always the idealistic thought that you simply won’t need it :)

However, this week I learned – the hard way – that logging is 1. an incredibly useful debugging tool and 2. that implementing logging is merely a matter of minutes. To give you a bit of context: I had a problem in a live application, so the normal processes of debugging were useless. I decided to implement logging hoping that would reveal the issue and guess what… it didn’t :). In the end, it was the brilliant idea of a colleague that made me see the light. But that’s not the point here, the point is that logging is a brilliant tool and that it definitely could help you troubleshoot your (live) application!

So, without further ado, I’ll explain how to implement Log4Net in web application that uses the .NET 2 framework.

1. Get the correct log4net.dll, you can find this (and all you need to know about log4net) on

2. Add the log4net.dll in the bin folder of your application and add it to your references.

3. In the web.config file add the following line of code in the configSections-tag within the configuration-tag. Make sure you add this as a section-tag and not within a sectionGroup-tag.

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

4. In the web.config file, add the following code after the configSections, appSettings and connectionStrings-tags within the configuration-tag (if you have all of these).

This configuration contains settings that define the type of log file, in which file to write the loggings, the maximum file size, whether to append or overwrite, the layout of a log line and much more. More information about this configuration can be found on

      <appender-ref ref="LogFileAppender">
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs/log.txt">
      <appendtofile value="true">
      <rollingstyle value="Size">
      <maxsizerollbackups value="10">
      <maximumfilesize value="10MB">
      <staticlogfilename value="true">
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n">

5. Add the following “using” statement to the code behind file of the page on which you want to add logging functionality.

using log4net;

6. Add the following code at the top of the class in the same code behind file to initialize the logger instance.

// initialize logger
protected static readonly ILog log = LogManager.GetLogger(typeof(NameOfClass));

7. Add the following code in the first method of the class in the same code behind file to configure the logger instance with the settings in the web.config file.


8. Add the following code to start logging information. You can log the following levels “Debug”, “Info”, “Warn”, “Error” and “Fatal”. These levels indicate the level of severity of the logged message. In your log configuration, you can define which levels should be logged.


9. Voorbeeld log file

DEBUG 2012-04-04 03:25:20 – ‘debug’-level
INFO 2012-04-04 03:25:20 – ‘info’-level
WARN 2012-04-04 03:25:20 – ‘warn’-level
ERROR 2012-04-04 03:25:20 – ‘error’-level
FATAL 2012-04-04 03:25:20 – ‘fatal’-level

That’s all folks, all you need for a working logging instance! As you can see this will only take you minutes to implement and it can really help you out. If learned my lesson and plan to implement logging as the first to-do on my next project.

Could be useful, right?

No comments:

Post a Comment