C#: Implementing NLog in your application


One time I have this application that was already deployed in the production area, the Business owner of the application informed me about the application encountered a system crash when certain operations were performed. I really had a hard time figuring what caused the abnormal termination because I didn’t implement a logging function. To make the story short, I learned my lesson that for every application developed, you should implement a logging function. So in today’s article I want to show you how to implement a NuGet called  NLog for you C# applications.

What is NLog ?

NLog is an open source and free logging tool for .NET platforms like Xamarin, Silverlight, WPF and Windows Store applications. NLog can generate different output formats like a text file, text console, email message, database event logs and much more. What I use more is the text file format which contains contextual information like the current date,
log levels, source name and the logged information e.g. (Stack trace, exceptions, process).

NLog Log levels

Now that we know what is NLog, lets discuss about the different Log Levels that NLog has:

Debug – This logging level is only used during development and testing stage of the application. There should be no debug logging level when the application is already in the production stage.

Information – This logging level is only used when you want to log important information which is useful for running and managing your application. This can also be used for the Entry and Exit points for the executed process.

Warning – This logging level is only used for logging of warnings that doesn’t appear in the user side like missing configuration settings.

Error – This logging level is only used for logging encountered error typically the unhandled exceptions. This kind or logging are usually contained in a Try Catch statement.

Fatal – This logging level is only used for scenarios wherein your application cannot function well like missing configuration files, terminated application or sometimes network disconnection.

Trace – This logging level is only used for detailed logic level program flow like what If the statement was executed, conditions that were tested.

Installing NLog to your project

You can install the NLogs NuGet either using the Package Manager Console or the Manage NuGet packages for solution as shown below:


Let’s try first installing the NLog using the Package Manager Console. So under the tools menu then NuGet Package Manager click the Package Manager Console. It will then load the Package Manager Console window as shown below:


Then just type install-package Nlog in the package manager console and wait for it to finish downloading online, then installing it in your project as shown below:


Let’s now try to install it using the Manage Nuget Packages for Solution. When you have already clicked the Manage Nuget Package for solution a window will prompt as shown below:


Just click the Online and All part in the left side of the menu then Search Online for the NuGet NLog and result will appear. The NLog was already checked because we have already successfully installed it earlier using the Packager Manager console. If it was not installed you will just select from the NuGet and click install as shown below:


Implementing NLog to your project

Now that we have already setup our NLog NuGet with our project lets try to create a simple application showing the different functionalities of NLog. For NLog by default it will look at the different configuration files in your application like App.config, Web.Config and NLog.Config but in our sample application will point the NLog configurations to our App.config file.

App.config Contents:

First things first, if you want your NLog to use your default application config file don’t forget to add the <configSections> settings in your App.config in order to properly setup your NLog as shown below:


Then for the actual NLog configurations which is contained inside an <nlog> </nlog> tags. It is divided into two, one is the <targets> tag and the other one is the <rules> tag.


The <targets> tag contains the configurations for the name which will be used in the rules section, the type which consist of different types e.g. (File, Console, Network) but for our sample application lets just use the File type. Next is the filename which is where the log file is located. The layout is the format of the data that is being written in our log file.

The <rules> tag contains the name which is in our case is just default value the minlevel is the lowest level e.g.(Trace,Information,Debug) allowed to be written in the log file and lastly the writeTo which points to the target’s name that we have created.

Sample Application:

For our sample application will just try to see the output of the log file using the NLog configurations that we have set in our Application Config file.


Sample Log File:

As you can see in the log file below NLog just written the Info type up to Fatal type because we set previously in our NLog configurations that our minimum level is Info. The format of the date time and log level in the actual log file was the same with the layout rule that we previously set in our NLog configurations.



You have seen the simple implementation of NLog in our C# application. What I Like about NLog is that it is versatile due to the targets and rules setting that we setup from the type of logs to be created, path of the logs, log name and level restrictions. For Complete documentation you can follow this link.

Happy Coding 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s