Nlog is a tracking code registration tool that supports several .NET platforms and the best, which is flexible and free (Open-Source), this tool provides the ability to control in detail the level of follow-up messages, such as show warnings and errors, NLog supports the following levels:
Trace: Very detailed log messages, potentially high frequency and volume.
Debug: Debug information, it is less detailed than the trace level, it is not normally enabled in the production environment.
Info: Informative messages, it is normally enabled in the production environment.
Warn: Warnings that do not appear for the user of the application.
Error: Error messages, most times they are exceptions.
Fatal: Fatal error messages. After a fatal error, the application usually ends.
Nlog is developed under the terms of the BSD-3 (Berkeley Software Distribution) license, which is permissive as the OpenSSL license or the MIT License. You can download the binary and source code versions from your website (https://nlog-project.org). It also provides a graphic installer that allows you to install Nlog in the place you prefer and integrate with Visual Studio 2016/2017.
In .NET it is clear that there are several tracking code registry tools such as Serilog, Log4net, ELMAH, Entlib, Nspring, etc. All have the same functionalities, but each one has an advantage over another because of usability in the code or speed in the execution of the registry. Nlog in addition to having the best performance and usability in the code, provides good community support with integrations for record management solutions such as Loggly (analysis and monitoring tool) also keeps a frequent update.
The use of this tool will help us in an important aspect for any application we do since it facilitates the sending of your records to different places changing their configuration and should not be treated as a late occurrence since we can analyze and audit the critical points of the system and solve them quickly.
As a result we will have a structured tracking code record and the developer will be able to debug without problems in modules published in the production environment since in some cases they do not have access to that environment.
To make this article more dynamic, we will give you the keys to the correct use of Nlog in the following program:
Using Nlog in Visual Studio
Nlog can be used in any project, whether large or complex or small, it also supports the following .Net platforms
.NET Framework 3.5, 4, 4.5, 4.6 and 4.7
.NET Framework 4 client profile
Windows Phone 8
Silverlight 4 and 5
ASP.NET 4 (NLog.Web package)
ASP.NET Core (NLog.Web.AspNetCore package)
.NET Core (NLog.Extensions.Logging package)
.NET Standard 1.x – NLog 4.5
.NET Standard 2.x – NLog 4.5
UWP – NLog 4.5
First we will create a console project in C # language in Visual Studio, by clicking on New project, we will look for the Visual C # language, select the Windows option and then select Console Application.
* The installation of the NuGet package manager will be used for the installation.
* Then we install the Nlog add-ons (registration platform) and Nlog.Config (file configuration for Nlog)
The Nlog plugin, installs all the functionalities and events for the management of follow-up messages.
The Nlog.Config add-on creates the configuration file for the use of the Nlog tool.
Automatically the Nlog.Config add-on generates the configuration file
Use and configuration
* Nlog supports 2 types of configurations.
Configuration in a web.config or app.config file just by adding 2 elements
1.- <configSections> and declaring the NLog reference
2.- <nlog> Here the configuration for the use of the tool is built.
Simplified configuration in a separate file.
* Configuration Elements
For the construction of the configuration file these elements must be used (required)
Saved in File
For saving in File, it is necessary to use the following configuration, where each element is required as:
name = (Name of the target)
fileName = (Path and file name)
layout = (Design and rendering of the message)
Then define the name of the rule, the rules for the use of record levels (trace, debug, info, warn, error and fatal) and in the property writeTo must go the same name of the target that was previously defined.
Then in our application we declare a Logger type object instantiating with the GetLogger method and the name of the rule that was defined in the configuration file
Next, we execute the program and we have this file generated.
In conclusion, the Nlog tool has all these capabilities giving a great support to the developer for the software to give great impact that require records of files, database, etc. and speed.