NLog in .NET Core: A Comprehensive Guide

An Tran
4 min readJan 31, 2023

--

NLog is a popular logging library for .NET Core applications that provides robust and flexible logging capabilities. Logging is a critical aspect of software development as it helps to track and diagnose errors, issues and performance problems in production. NLog in .NET Core provides several features such as rich log customization, multiple logging targets, and high performance.

In this article, we will discuss the installation, configuration, and use of NLog in .NET Core applications, along with best practices for using it effectively.

Installation of NLog in .NET Core To start using NLog in .NET Core, you need to install the NLog library. To do this, you can use NuGet, a package manager for the .NET platform. Here are the steps to install NLog in .NET Core:

Prerequisites: You need to have .NET Core installed on your machine. You can download it from the official .NET website.

Downloading NLog library: Open the Package Manager Console in Visual Studio and type the following command to install NLog:

Install-Package NLog.Web.AspNetCore

Integrating NLog into .NET Core project: Once NLog is installed, you need to integrate it into your .NET Core project. To do this, you need to add the following code in the Startup.cs file:

using NLog.Web;

// ...

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...

app.UseNLog();
}

Configuring NLog in .NET Core Once NLog is installed and integrated into your .NET Core project, you need to configure it. The configuration of NLog is done in a file named nlog.config. Here are the steps to configure NLog in .NET Core:

Creating NLog configuration file: You need to create an XML file named nlog.config in the root directory of your .NET Core project.

Setting up target and layout: In the nlog.config file, you need to define the target and layout for your logs. The target is where you want to store your logs, and the layout is the format of your logs. For example, you can store your logs in a file and define the layout as follows:

<target name="file" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${level} ${message}" />

Defining logging rules: Finally, you need to define the logging rules in the nlog.config file. The logging rules determine what log messages will be written to the target. For example, you can write all log messages with the level of Error or higher to the file target:

<rules>
<logger name="*" minlevel="Error" writeTo="file" />
</rules>

Using NLog in .NET Core Now that NLog is installed and configured in your .NET Core project, you can start using it to log messages. Here are the steps to use NLog in .NET Core:

Logging messages in code: You can log messages in your code using the NLog logger. To do this, you need to create an instance of the NLog logger and use the appropriate logging method, such as LogInformation, LogDebug, LogError, etc. For example:

private static readonly ILogger _logger = LogManager.GetCurrentClassLogger();

// ...

_logger.LogInformation("This is an informational log message");

Exception handling and logging: You can log exceptions in NLog by using the LogError method. You can also pass in the exception object to the LogError method to get more detailed information about the exception. For example:

try
{
// some code that throws an exception
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while processing the request");
}

Customizing logs: You can customize the logs in NLog by using the layout attribute in the target definition. The layout attribute allows you to specify the format of the log messages. For example, you can include the date, time, and log level in the log message by using the following layout:

layout="${longdate} ${level} ${message}"

Best Practices for using NLog in .NET Core To get the most out of NLog in .NET Core, it is important to follow some best practices. Here are some best practices for using NLog in .NET Core:

Logging levels: It is important to choose the appropriate logging level for your log messages. NLog provides several logging levels, such as Trace, Debug, Info, Warn, Error, and Fatal. It is recommended to use a higher logging level (e.g., Error or Fatal) for important messages and a lower logging level (e.g., Debug or Trace) for less important messages.

Logging performance: Logging can have an impact on performance, so it is important to choose a logging target that has good performance. For example, using a file target is generally faster than using a database target.

Storing logs securely: If you are storing logs in a file or database, it is important to ensure that sensitive information is not stored in the logs. You can use the NLog layout attribute to exclude sensitive information from the logs.

NLog is a powerful logging library for .NET Core applications that provides robust and flexible logging capabilities. By following the steps outlined in this article, you can easily install, configure, and use NLog in your .NET Core applications. Additionally, by following the best practices for using NLog, you can get the most out of this library and ensure that your logs are useful and secure.

--

--