Basic Understanding Of NLog

I will also describe how and where to use Nlog. I hope this article will really help those who have never used Nlog. Let’s start digging into this.

What is Nlog ?

Nlog is an open source log platform. Through this platform, we can log an activity of an application. This makes an application's code easy to handle. Nlog provides a cross platform support.

Things we can log

By using Nlog in an Application, we can log the things given below.

  • Trace - Detailed Log
  • Debug - Debugging Information
  • Info - Info Message
  • Warning - Warning Messages
  • Error - Error Messages
  • Fatal - Big Errors, which needs attention

Basic Targets

  • Files- We can log our messages into a file.
  • Database can also work with SQL Server, using Nlog.
  • Console displays messages in a C# Console Application.
  • Email- We can email log message.

Today, we will create a console Application and will use Nlog for the things given below.

  • To display log messages in a console application.
  • Log messages in a file at a specified location.
  • Also, we will email a logged message.

Let's start.

Step 1

Create a C# console Application and name it NlogDemoApplication.


Step 2

Install Nlog package in the Application. You can use NuGet Package Manager for this. Use the command given below.

Install-Package NLog.Config


Two files will be added, as shown below.

  1. Config
  2. xsd

Step 3

Open Program.cs file and create a private static Logger per class. We also need to use LogManager to create Logger instances. Do not forget to add namespace as well.

  1. private static Logger logger = LogManager.GetCurrentClassLogger();  

Step 4

Replace the main function with the code given below. In this method, we just call Nlog error function to log a static error message.

  1. static void Main(string[] args)  
  2.             {  
  3.                     logger.Error("This is an error message");  
  4.                     Console.Read();  
  5.             }   

Step 5

Now, open Nlog.Confile and replace code given below as well.

  1. <targets>  
  2.     <target name="console" xsi:type="Console" layout="${longdate}|${message}"/>  
  3. </targets>  
  4.   
  5. <rules>  
  6.     <logger name="*" minlevel="Error" writeTo="console" />  
  7. </rules>  

Under targets tag, we add keys as targets; i.e. where we want the results and under rules tag and we add key (rules), when we write our log. In the meantime, we are going to target a console Application and we are going to write a log whenever an error will occur.

Note

  • xsitype Type of target for log
  • Layout Output
  • Name name of the target

Step 6

Run the Console Application and you will see the output given below. This is how we display a log message to a console output Window

 

Step 7

Now, we going to target a file to log an error message. Again, open Nlog.Config file and add line given below under targets tag.

  1. <target name="file" xsitype="File" fileName="D\logs\NLog.log" layout="${longdate}|${message}"/>   

In the existing rule, add the file (name of the target) with the console. Now, your code will look like, as shown below.

  1. <targets>  
  2.     <target name="console" xsitype="Console" layout="${longdate}|${message}"/>  
  3.      <target name="file" xsitype="File" fileName="D\logs\NLog.log" layout="${longdate}|${message}"/>  
  4. </targets>  
  5.   
  6.   <rules>  
  7.     <logger name="*" minlevel="Error" writeTo="console,file" />  
  8.   </rules>  

Step 8

Run the code. Now, you will see the output in two places, where one is in console Window and the second is in the target file. Check the path specified in the file target. See the image given below for the reference.


Step 9

We are going to target an email now. Again, add one more target (as shown below) under targets tag.

Type Mail
Subject Could be whatever you want
ToTo whom you will send email
From Sending of email
Body layout (Message you want to display  in email)
enableSSl For secure email
SmtpAuthentication (None, Basic, Ntlm)
SmtpUserName Username (from email)
SmtpPassword Password (From email)
SmtpServer Server from which you want to send the email.
Smptport As per servers

We are using basic authentication, so we have to mention smtpusername and password.

  1. <target name="sendMail" xsitype="Mail"  
  2.           subject="Application Error Log"  
  3.           to="XXXXXXXXXXXXXXXXXXXXXXXXXXX"  
  4.           from="XXXXXXXXXXXXXXXXXXXXXXXXX"  
  5.           body="${longdate}|${message}"  
  6.           enableSsl="true"  
  7.           smtpAuthentication="Basic"  
  8.           smtpServer="smtp.gmail.com"  
  9.           smtpUserName="XXXXXXXXXXXXXXXXXXXX"  
  10.           smtpPassword="XXXXXXXXXXXXXXXXXXXX"  
  11.           smtpPort="587"/>  
  12.   </targets>   

The name of the target, which is sendEmail and the existing rule is shown below.

  1. <logger name="*" minlevel="Error" writeTo="console,file,sendMail" />   

Step 10

Run the application. In my case, I see the output, as shown below. You can check the result on your specified email address, when you try this code.

Thus, you can check the log messages now at three places
  • Console
  • File
  • email

In this example, I made it simple, so that the basic concept could be clear to all. I just used an error message. Similarly, you can log an info message, fatal error, trace and so on. You may also do many more things with Nlog, as it all depends on your creativity and effort. There are lots of things which we can do using Nlog. I just showed here how to use it. I hope you liked the article. 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now