Click here to Skip to main content
13,596,314 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 6 Jan 2007

Logging with NLog

, 6 Jan 2007
Rate this:
Please Sign up or sign in to vote.
The article describes how to configure nlog to log reports as per your requirement
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


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.


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)


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.



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


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

Generalnot great Pin
CIDev2-Dec-10 5:24
memberCIDev2-Dec-10 5:24 
GeneralMy vote of 1 Pin
bluesoniq7-Jul-10 23:03
memberbluesoniq7-Jul-10 23:03 
QuestionWhat about viewing/analyzing the logs NLog creates? Pin
Jay Cincotta27-Nov-09 3:43
memberJay Cincotta27-Nov-09 3:43 
QuestionNlog with log2console Pin
dany paredes8-Jun-08 13:36
memberdany paredes8-Jun-08 13:36 
QuestionQuestion: Is NLog Thread Safe? Pin
Doug K. Wilson7-Jan-07 5:02
memberDoug K. Wilson7-Jan-07 5:02 
AnswerRe: Question: Is NLog Thread Safe? Pin
hacked.emotions7-Jan-07 5:30
memberhacked.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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 6 Jan 2007
Article Copyright 2007 by hacked.emotions
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid