NLog logger in .NET Project : Tutorial

Recently, I was working on an enterprise project using Windows Forms and needed a logger that was thread-safe, allowed both structured and text based logging and provided an options to integrate email sending as well. Two options came standard NLog or log4Net.

Had a brief look at both. log4Net looked like more of XML configuration. NLog looked like less so. Went with NLog.

How to add NLog to your .NET solution:

Scenario is that we have multiple projects in our solution.

Step 1) Install NLog.Config through Nuget package manager. This will automatically install other dependencies like NLog and NLog.Schema. Please do this in your StartUp project.

Step 2) Step 1 will add a file called NLog.config (we don’t modify another NLog.xsd file). Our goal is to set a new log file each day with date in it’s name and email notifications in case of warnings and (fatal) errors.

With ${shortdate} in the file name, it will automatically make a new logfile everyday.

Then, in which ever file you want to do logging, do like this (in my example, I am showing in Program.cs file)

Note: Only lines 6 and 12 are important. Rest are added for testing NLog. Once again, whichever file needs logging, add lines 6 and 12 in it.  After you run this app, it will generate a logfile in the directory configured in NLog.Config (like in C:\LPLogs) with the dummy log data we put from lines 15 and 27.

If you want to log in other projects within the solution, just reference NLog.dll directly in each of the projects and use it. But now you only need to configure NLog in your main application. The configuration will then be shared automatically across all projects that reference NLog.dll from the main application.

 




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *