5. NLog

NLog started in 2006 and it is also open sourced. There is a NuGet package available, and everything is configurable via XML. After installing & importing the nuget package, we need to configure the framework. We read the NLog configuration file. and in this configuration file, we setup one or more targets, a target is for instance a flat file, the windows event log.

In our demo we setup a flat file logging.log and the console as logging output. The configuration is done only once in the startup of the application. Next we create our logger, which needs to be created in every class where we want to log something. We create our logger by calling the LoadConfiguration method of the LogManager class of Nlog.

We pass as parameter the configuration file. Next we create a log instance by calling the GetCurrentClassLoggerm method of the logmanager class. Now we can start logging to our output. You can see we output to the console as well as to a text file.

Project: https://github.com/nlog/NLog/

https://nlog-project.org/

NuGet: https://www.nuget.org/packages/NLog Configuration: Configuration is done with an XML file.

Example code:

you can download the project from the Files menu

internal class Program 

{ 

    private static void Main(string[] args) 

    { 

        LogManager.LoadConfiguration("nlog.config"); 

        var log = LogManager.GetCurrentClassLogger(); 
 

        log.Debug("Starting up"); 

        log.Warn("This is a warning"); 

        log.Debug("Shutting down"); 
 

        Console.ReadLine(); 

    } 

}