As soon as the application works your customer will find the first bugs. In this case it is very important to find out what happened and to find the mistake. Because of this it is helpfully to include a logging while you are debugging.
Log4Net is a very classy library which fulfils you every desire and it takes just a few minutes of work.
Log4Net
Log4Net is a quite practical .Net library which is created to make logging easier. There are several different types of "log steps" (debug, error, info,..) and loggings ("Appender"). So for example it´s possible to log into the Visual Studio Debug Window or in a file and so on. It´s also possible to configure it via XML so you are allowed to create log steps on the productivity system.
You can find a good introduction on the homepage of Log4Net.
Practical entrance
To show you the whole problem on a very easy way I´m going to create a consol program and integrate Log4net DLL. Here you will find the "log4net.dll"
Configuration of Log4Net
The easiest way to configure Log4Net is to use App/Web.Config. Therefore we need to create an "App.config" file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="DebugAppender" type="log4net.Appender.DebugAppender" > <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> <root> <level value="All" /> <appender-ref ref="DebugAppender" /> <appender-ref ref="ConsoleAppender" /> </root> </log4net> </configuration>
Explication:
We define us our own ConfigSections, so it´s possible to control everything central.
In this log4net-section we are going to use the different types of "Appender". Depending on the Appender there are several ways to log. For example the "ConsoleAppender" logs on the commando line and the "DebugAppender" on the Visual Studio Output Window. Other types of Appender you will find here: Config Examples.
In every Appender it´s possible to guideline the layout so you can adapt the Log Message.
In the last step we chose the level which should be logged - for our example everything, and we are going to use the two appenders.
"Logging Code"
class Program { static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); ILog logger = LogManager.GetLogger(typeof (Program)); logger.Debug("Hello World!"); logger.Error("D´oh!"); Console.ReadLine(); } }
In Line 5 we dispose Log4Net to check the XML config and than we get our Logger. The Logger has a method for every "Log Level".
Conclusion:
If I´m going to run the application now there is following Output on the console:
And on the Output Window in Visual Studio:
Where exactly should I log?
I didn´t find any general rules but you should keep in mind that the sense of logging is to localise mistakes. So for example you could logout the parameter or "important calls from other services" and the output. With this it´s easier for you to get a feeling for the Code.
Especially with the "DebugAppender" it´s kind of funny if you press a button and you can see how the request walks to the levels and logg out the values. Nerd paradise.