Tracing and logging in .Net
February 11, 2009
Code file : TestLog
Introduction
The situation is, you?re working in the office and your boss rush in and ask you. He said: ?Some customers just phone and they can?t register our website, what the problem??. What are you gonna say? I don?t know, I will check on it
. Well that?s not sound so cool. What about, yeah, that?s the problem with a query, I?m fixing it now. Now, you?re an expert, you know everything going with your system
.
Another example, your friends ?re working and suddenly they saw you tidying up your table. They look at you bizarrely
and ask themselves what the hell are you doing? Just a minute after that, the beautiful girl from the sale department comes in the office and before she could say anything, you make the best smile you could ever make and say: Oh, I know your problem, let me show you how to fix it. Cool right
.
Just kidding, it only demonstrates how important (and awesome
) of the tracing and logging system is.
Tracing, logging system and NLog
The most basic use of logging framework is sure to store all the error messages and maybe send it back to the developer. Well, how easy that is, should we write it ourselves? Yeah, we could but why should we when we have a framework do it for us and someone else make sure it?s stable and fully test already. Just think if you want to write to a file which is simple. However if your company has 100 computers and they all log to a single file, what will happen when they are all start writing at the same time??? Well it?s easy, put it in a queue and this and that… trouble. So just use a system which does all that for you.
There are so many logging frameworks out there such as Log4Net, Nlog or logging framework (from The Object Guy). Each has their own strong and weak points. For myself, I prefer to use NLog, reason is: it?s so simple and easy to reuse.
You could download the NLog from http://www.nlog-project.org/download.html
Some document: http://www.nlog-project.org/documentation.html
Start with NLog ? Log to file
If you downloaded the NLog, you will find there are a lot of config example in there.
The most basic logging is store all the trace and error log to a file.
To using NLog, you start with configure for NLog. I normally store it in a file, and then I could just copy it over to a new project when I start again.
Step 1:
Set up NLog into your system.
Add NLog.dll to the your project?s references. You could find it in .Net Section of Add Reference…
Step 2:
Create an Nlog.config file in your project folder.
You also need to set it up to copy if newer.
Depend from your requirement, this is how my config file normally look like (for file only):
fileName="${basedir}/logs/debugfile.txt"
keepFileOpen="false"
encoding="iso-8859-2" />
fileName="${basedir}/logs/tracefile.txt"
keepFileOpen="false"
encoding="iso-8859-2" />
There are two target sections, target is set where and you want to save the log.
As we want to save it to file, I set: xsi:type=?File?.
Layout of the the log is set in layout.
File name is where you want to save the file.
keepFileopen = false to make sure it close the file after finish append a new log.
I normally keep it the same in every file target just change the filename section.
Rules section is where you set when you want to log. minLevel is for and maxlevel is set when it start logging. You could use singlelevel so it only occur when the exact level happen. Bellow is the list of level (From NLog website)
? Trace - Very detailed log messages, potentially of a high frequency and volume
? Debug -Less detailed and/or less frequent debugging messages
? Info - Informational messages
? Warn - Warnings which don't appear to the user of the application
? Error - Error messages
? Fatal - Fatal error messages. After a fatal error the application usually terminates.
WriteTo is set to one or many target you set in the targets sections above.
Step 3, in the project:
Assume you have your project and now you want to insert a logging system in. I made a very simple and unstable project for this test. It has 3 textboxes and 1 button, you click the button, it sum up two first textbox and show the result in the 3rd textbox. No checking if the input is a number or not.
Now if you enter a non-number value in one of the text box you will have an error message ?Input string was not in a correct format?. That?s not what we want. Let?s put the command inside a try catch.
The user will have a friendlier message and make things look more under control.
Step 4:
Now we want to log that message to debug file. You will need to declare a Logger variable (could make it global if you want).
Then in the catch, change into :
Run the app and enter some invalid data. You will receive the message box about the error. Check in the execute folder, you will find you have a new ?log? folder with the debugfile.txt in there. Open it to see what in there.
Step 5:
Say I also want to know how many time and what user do in my app. I change the content inside the try:
Now, every you click ?+? button. It store the textbox contents into the TraceFile.txt.
CONCLUSION
Now, it?s very very basic things in logging. I myself once have to Google around to find a suitable Logging and tracing framework so I hope this article could help someone out. I also will try to write another article about logging to E-mail and database as soon as possible.

Leave a Reply