Thursday, March 19, 2009

Logging Errors with NLog a .NET Logging Library

NLog is a .NET Logging Library, 

Official Site mentions it as follows,
NLog is a .NET logging library designed with simplicity and flexibility in mind. With NLog you can process diagnostic messages emitted from any .NET language, augment them with contextual information, format them according to your preference and send them to one or more targets.
Using it is very easy. Following steps helps you to get started.
(For Visual Studio 2008, Web Applications)
1. Download and install NLog Library
2. If you are using Visual Studio 2008, follow this step as mentioned in my earlier post, to fix issue with Intellisense.
3. Add reference to NLog.dll.
4. Add the following Entries to web.config


5. Add  following entries to your code behind file
(for C#)
using NLog;
using NLog.Targets;
using NLog.Config;
using NLog.Win32.Targets;

6.  Create a Logger object as below in your code behind file

catch(Exception ex)
{
    Logger errLogger = LogManager.GetLogger("*");
                    errLogger.Debug(ex.Message);
}


7. If you see, i have passed * as an argument to GetLogger method, please refer web.config entry, i have mentioned logger name as *.

8. This will log exceptions to  /Logs/Log.txt

If you encounter any problems then let me know.

No comments: