Many of the systems, we develop today needs some kind of a logging/tracing mechanism to identify issues or any kind of information.

↑ Return to Top


1. NLog

NLog is a free logging framework to log various kind of messages into a specified place. We can use NLog in a .NET environment, as well as with Xamarin and Windows phone.

↑ Return to Top


2. Logging

If we want to log a message from our application, We can use EventViewer without any libraries. But If we want to share the log file with someone else and analyse it in the future, we can’t do that with EventViewer.

So we have to go for a logging framework if we want to write messages into a file or a database.

↑ Return to Top


2.1. Logging Frameworks

We can use any of these frameworks for logging purpose, Log4net, NLog, ELMAH, Microsoft Enterprise Library, NSpring.

ELMAH is a web logging framework, All the other frameworks can be used with any .NET type of application.

When we use NLog or Log4net for logging, it takes just few minutes to integrate it to the application, But performancewise its not the same. NLog is much faster than Log4net and as well as all the above mentioned logging frameworks.

NSpring also easy to setup, but it requires more coding to log a message. With a large scale enterprise application, It will be tedious to work with NSpring.

Microsoft Enterprise Library (EntLib) is much faster, But need heavy configurations and coding to work with it. ELMAH is a web logger, It’s going to log the messages into a xml file by default.


If you application needs file logging and more performance, better to go with NLog or NSpring. We can use Log4net also, it’s not that much faster, but easy to setup and use it in the application

↑ Return to Top


2.2. Install NLog

Latest stable version of NLog is NLog 4.3.5.

With Nuget Package manager, I installed NLog,

NLog installation

↑ Return to Top


2.3. NLog Targets

NLog targets are used to show, store or pass the message into another destination. Some commonly used NLog targets are shown below,

  • Console : writes message to a console window.
  • Event Log : writes message to event viewer.
  • Debugger : writes messages to the attached debugger.
  • File : writes log message to a file.
  • Database : store log message in database.
  • Memory : can write log message into a arraylist in memory.
  • Mail : send log message by email.
  • Method call : For log message, calls specific static method.
  • Network : send log message over a network.
  • Service : calls a web service for each log message.

And if these targets don't satisfy your requirements, you can create your own targets as well, (custom target)

↑ Return to Top


2.4. Target Layout

Target layout means how our log message is going to display, in which format, which order. We can create custom layout renders as well. Commonly used layouts are as follows,

  • ${callsite} : Class name, method name or source information of the log message.
  • ${callsite-linenumber} : line no of call site source.
  • ${date} : date and time
  • ${exception} : exception details
  • ${level} : log level
  • ${logger} : logger name
  • ${longdate} : date and time in long format.
  • ${message} : log message.
  • ${stacktrace} : stack trace

↑ Return to Top


2.5. NLog rules

NLog rules are called as a log routing table. It’s going to matches with a target and writes into a log with specified layout. Some of the attributes in NLog rules,

  • name : logger name
  • minlevel : minimum log level for a rule to match
  • maxlevel : maximum log level for a rule to match
  • level : single log level for a rule to match
  • levels : comma separated list of logs for a rule to match
  • write to : comma separated list of targets to be written when a rule matches
  • final : no further rules are processed when this rule matches
  • enabled : going to disable/enable rules

↑ Return to Top


2.6. Nlog levels

One thing to note, In these level attribute, log messages are ordered from type. Trace is the minlevel log type, then Debug, Info, Warn, Error and Fatal.

  • Trace level can be used when we need to notify the beginning and end of a method
  • Debug logs, we can identify whether session is expired, user is authenticated
  • Info logs are used for more generic scenarios like, Email sent.
  • Warn level can be used to notify warnings
  • Error logs are used for exceptions and when application crashes.
  • Fatal logs are highest level, used for most important cases.

↑ Return to Top


2.7. Let’s see what’s in action

In configuration file, I have configured log type and log level, I have used file logging, Trace as a minimum logging level.

configuration file

In code, I have written like this,

code i have written

Let’s check how it’s written in the log file

output seeems like this

In output, we can see the layout has been applied, ${longdate} ${callsite} ${callsite-linenumber} ${date} ${level} ${message}

↑ Return to Top


2.8. How to use multiple targets

Let’s see how to add multiple targets for logging

configuration file

In configuration file, i added two targets, one is to log into a file, other target is to log into a console window

console output

File logging output is same as previous output, console output is seems like this. In console logging, I used a different layout like this, ${date} ${callsite} ${level} ${message} and minimum log level is added as Warning level, So output got changed like this,

↑ Return to Top


3. Download

3.1. TechNet Gallery


↑ Return to Top


3.2. GitHub

↑ Return to Top


4. Conclusion

This article explains how to log a message to a console window and a text file using Nlog multiple targets. It provides a deep dive into various logging frameworks available and performance, configurations and implementation of it.

↑ Return to Top


5. References

↑ Return to Top