Tuesday, August 11, 2009

Log4net: Logging all the events.

Debugging is not always possible in the production environment. However, there are many exceptions thrown and we intend to see the flow of control using debugging statements.
One of the crude ways to do this is by printing debug statements to console. Little improvised way to do this may to print all the appropriate messages to a specific file.
But what if you want to track who printed that message(whcih object, method,) and you want to capture all the information in an organised way. you want to print a message to sql database or send it as an email. well we are programmers we can easily write code which does that. But think about the nuber of bugs the new code will induce into the project. Think about the number of hours you invest in writing, testing this functionality.
Thanks to open source we have a api which enables us to have this logging functionality in our applications. there are many apis out there but I decided to go with log4net. This is a dotnet version for the famous log4j.
I had a lot of trouble setting it up not that it was complex it was as easy as filliing out a medical school application (just kidding). Trust me it is very easy and there are many examples configuration code readily available for use. There were two methods to configure log4net in my application. One is that i have to write a section in log4net and the other is that I can have a seperate xml for the configuration settings. The current available versions of log4net was not built for 3.5 so I suspect that was the resaon the former method of configuring the application for dotnet was not working, I was getting warnings which I initially ignored. I ran some test log commands and it did not work the text was not created. then I suspected it was some file permissions problem. But when I created a seperate xml file consisting of the settings it all the warning messages disappeared and it worked like a charm.
By using the tested open source products like log4net. I need not worry about the bugs (if it is well tested and matured of course). It will save a lot of time required to develop this functionality.