Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / C#
Article

Logging with NLog

Rate me:
Please Sign up or sign in to vote.
1.68/5 (13 votes)
6 Jan 20073 min read 52.2K   69   20   6
The article describes how to configure nlog to log reports as per your requirement

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

Image 1

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.

Image 2

I hope the article helps.

HAPPY LOGGING

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalnot great Pin
BillW332-Dec-10 5:24
professionalBillW332-Dec-10 5:24 
GeneralMy vote of 1 Pin
bluesoniq7-Jul-10 23:03
bluesoniq7-Jul-10 23:03 
QuestionWhat about viewing/analyzing the logs NLog creates? Pin
Jay Cincotta27-Nov-09 3:43
Jay Cincotta27-Nov-09 3:43 
QuestionNlog with log2console Pin
dany paredes8-Jun-08 13:36
dany paredes8-Jun-08 13:36 
QuestionQuestion: Is NLog Thread Safe? Pin
HellfireHD7-Jan-07 5:02
HellfireHD7-Jan-07 5:02 
AnswerRe: Question: Is NLog Thread Safe? Pin
hacked.emotions7-Jan-07 5:30
hacked.emotions7-Jan-07 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.