Introduction
While working with log4net(an old logging horse) i had to compromise on several of my needs. So i went on with my search to find another logging library that could satisfy my requirements. Then I Found NLog, a savior. NLog is a logging library which not only logs what you want to but also provides us options to log BETTER.
Although it has a no of features, but the features which sets it apart from other logging library are:
-You can implement window identity based file logging, i.e each person logging into windows can have log file created on his name.
-Automatically reload Nlog configurations from the config files, if at runtime some configurations were changed.
-provide a faciltiy of retry on error.
-And the best of all throw/consume exceptions.
Implementation
NLog can be configure in two ways, via config files or via code. Its recommended that we use config files, because in this way we get the liberty to change things without compiling the code.
Configuration Settings
Unlike other tools, NLog attempts to automatically configure itself on startup, by looking for the configuration files in some standard places. The following locations will be searched when executing a stand-alone *.exe application:
-standard application configuration file (usually applicationname.exe.config)
-applicationname.exe.nlog in application's directory
NLog.config in application's directory
-NLog.dll.nlog in a directory where NLog.dll is located
file name pointed by the NLOG_GLOBAL_CONFIG_FILE environment variable (if defined)
In case of an ASP.NET application, the following files are searched:
-standard web application file web.config
-web.nlog located in the same directory as web.config
-NLog.config in application's directory
-NLog.dll.nlog in a directory where NLog.dll is located
file name pointed by the NLOG_GLOBAL_CONFIG_FILE environment variable (if defined)
We will see how it is configured using config files,below is an example of a config file which is used to log information to a sql server database. The follwong configuration can be saved in NLog.config
Although most parts of the config file are self explanatory, Some points which are important.
-The first tag, which mentions autoreload=true signifies that whenever a configuration is changed at runtime it is reflected in the application without restarting it.
-Targets are nothing but places where log messages are written,they can be file,database etcs..
-The rest of the setting deals with the with setting up the database target, which is mostly concerned with database name,username etcs (generally done in connection strings).
-The other most important feature are the rules, the first rule signifies that all messages from any class whose level is Debug or higher are written to the "database" target. While the second rule signifies that,messages from any class in the Name.Space namespace whose level is between Debug and Error (which makes it Debug,Info,Warn,Error) are rejected (as there's no writeTo clause) and no further rules are processed for them (because of the final="true" setting)
Code
Coding is a cake walk, all you need to do is include a reference of nlog in your project, use the nlog namespace and write the below code to get started.
I hope the article helps.
HAPPY LOGGING