Click here to Skip to main content
Click here to Skip to main content

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-->

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

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

Share

About the Author

hacked.emotions
Web Developer
India India
No Biography provided

Comments and Discussions

 
Generalnot great PinmemberCIDev2-Dec-10 5:24 
GeneralMy vote of 1 Pinmemberbluesoniq7-Jul-10 23:03 
QuestionWhat about viewing/analyzing the logs NLog creates? PinmemberJay Cincotta27-Nov-09 3:43 
QuestionNlog with log2console Pinmemberdany paredes8-Jun-08 13:36 
QuestionQuestion: Is NLog Thread Safe? PinmemberDoug K. Wilson7-Jan-07 5:02 
AnswerRe: Question: Is NLog Thread Safe? Pinmemberhacked.emotions7-Jan-07 5:30 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 6 Jan 2007
Article Copyright 2007 by hacked.emotions
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid