NLog in a console application

 

Implementing NLog in a console application is a straightforward process. Here are the steps to set up NLog in a C# console application:

Step 1: Install NLog Package

First, you need to install the NLog package in your console application. You can do this using the Package Manager Console or the .NET CLI. Open the Package Manager Console and run the following command:

bash
Install-Package NLog

Or, if you prefer using .NET CLI, run:

bash
dotnet add package NLog

Step 2: Configure NLog

Create an NLog.config file in your console application project. This file will contain the configuration for NLog. Here's an example configuration that writes log messages to a file:

xml
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="info" internalLogFile="internalLog.txt"> <extensions> <add assembly="NLog.Web.AspNetCore" /> </extensions> <!-- the targets to write to --> <targets> <!-- write to file --> <target xsi:type="File" name="alldata" fileName="logs/${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <!-- another file log. Uses some ASP.NET core renderers --> <!--<target xsi:type="File" name="otherFile-web" fileName="logs-Others/${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />--> </targets> <!-- rules to map from logger name to target --> <rules> <logger name="*" minlevel="Trace" writeTo="alldata" /> <!--Skip non-critical Microsoft logs and so log only own logs--> <logger name="Microsoft.*" maxLevel="Info" final="true" /> <!--<logger name="*" minlevel="Trace" writeTo="otherFile-web" />--> </rules> </nlog>

In this configuration, log messages of level Debug and above will be written to the logfile.txt file inside the logs directory.

Step 3: Use NLog in Your Console Application Code

In your C# code, import the NLog namespace and create a logger instance like this:

csharp
using NLog; class Program { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { Logger.Info("This is an info message."); Logger.Warn("This is a warning message."); Logger.Error("This is an error message."); Logger.Fatal("This is a fatal message."); Console.WriteLine("Logging done. Check the log file for output."); } }

Note:





In this example, the logger instance Logger is used to log messages at different levels (Info, Warn, Error, and Fatal). These log messages will be recorded according to the NLog configuration specified in the NLog.config file.

Comments

Popular posts from this blog

Sites.Selected | Graph API SharePoint Permission

Configure the SharePoint Online App Catalog

Azure Function | Sharepoint List item | Call from Power Automate Flow