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 http://logging.apache.org/log4net/.

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.


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 http://logging.apache.org/log4net/.

  
    
      
    
    
      
      
      
      
      
      
      
        
      
    
  

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.

log4net.Config.XmlConfigurator.Configure();

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.

log.Debug("'debug'-level'");
log.Info("'info'-level");
log.Warn("'warn'-level");
log.Error("'error'-level");
log.Fatal("'fatal'-level");

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