Share via


Log messages using NLog framework

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 analyze 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 weblogging 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 a few minutes to integrate it to the application, But performance-wise it's 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 needs heavy configurations and coding to work with it. ELMAH is a web logger. It’s going to log the messages into an XML file by default.

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

The latest stable version of NLog is NLog 4.3.5.

With Nuget Package manager, we installed NLog,

↑ Return to Top

2.3. NLog Targets

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

  • Console: writes a message to a console window.
  • Event Log: writes a message to the event viewer.
  • Debugger: writes messages to the attached debugger.
  • File: writes a log message to a file.
  • Database: store log message in database.
  • Memory: can write log message into an ArrayList in memory.
  • Mail: send log message by email.
  • Method call: For log message, calls a 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 the 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 by 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 an application crashes.
  • Fatal logs are the highest level, used for most important cases.

↑ Return to Top

2.7. Let’s see what’s in action

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

In code, it's written like this,

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

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

In the configuration file, we added two targets, one is to log into a file, another target is to log into a console window

File logging output is same as previous output, console output it seems like this. In console logging, we 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

↑ 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