Friday, February 4, 2011

Logging - Oh ya Log4Net

One of the most important aspects to a good application in fact one of the most pivotal components happens to be logging. If not for logging it would be very nasty to troubleshoot an issue when the application goes live onto production. Where do you start looking for problems??

There are some really awesome logging frameworks out there but I am going to focus on an Open Source logging framework which i use it with .Net viz Log4Net. It's very easy to incorporate this feature onto your application and the best part it's free. Log4net is part of Apache Logging Services project which was intended to provide cross language logging services primarily for debugging.

I think I've given enough funda for now and i am going to jump into the essentials which one needs to do to incorporate Log4net as part of their application in this post. I was actually brooding over setting up this for my application 3 months ago and when i looked up i found most of the articles i read about setting this up were not properly documented. I decided to make a post about this properly so it might help someone in the future when they read this.

Before i start developing the sample application. Log4net can be downloaded from here, You can get the latest Log4net build from this site. It contains the Log4net dll which will be referencing for the sample application i develop.

I will explain this with a simple Forms application in Visual Studio. Create a Forms application on Visual Studio and once the application is created, Add a new config file to the application namely App.config and i think the abstraction would be the scenario that application would have a config file and in case it has many modules all the modules in turn would have their App.config files seperately. Before you use log4net you need to create a configuration setting for it in App.config file. I've listed the App.config setting below


  
    

The main thing to note is the param tag by name File. This contains the destination where the log file will be created for instance in this scenario it will be created under the folder MyLogFolder with the log file name as customlog.txt. Now i will focus onto the Sample Application part and how to go about accessing the log4net from the application side. The application like i mentioned is a simple forms application with a button click event as shown in the snippet below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;

namespace Log4Net
{
    public partial class Form1 : Form
    {

        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);  
        public Form1()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log.Warn("Custom Warning Message");
            log.Debug("Custom Debug Message");
            log.Info("Custom Info Message");
            log.Error("Custom Error Message");
        }


    }
}


The code snippet is very elementary. It shows the way to incorporate the log4net after adding the reference to the DLL via the application. The main thing to note would be lines 17 & 21 which are both self explanatory in nature and on the click of the button the log file would've have the following contents as shown below.

WARN 2011-02-04 12:03:07 – Custom Warning Message
DEBUG2011-02-04 12:03:07 – Custom Debug Message
INFO 2011-02-04 12:03:07 – Custom Info Message
ERROR2011-02-04 12:03:07 – Custom Error Message


Like i said it's just a skeleton and it's a very simple framework with very strong functionality embedded in it. Logging made easy!!

2 comments:

  1. Thanks for this article on log4net. I have been searching this for a while. Have you used the other variations which can be used in c++ ? (log4cpp)

    ReplyDelete
  2. I have zero knowledge of C++

    ReplyDelete