Side note: Centralize your logs with Better Stack
Head over to Better Stack and start shipping your NLog logs in minutes with our native .NET integration.
NLog is a logging framework for .NET. It has rich log routing and management capabilities and greatly helps you to produce and manage logs. NLog supports structured logs, multiple logging targets, and everything a modern logging framework should support. In the long list of the NLog's features, you can find:
@ operatorNLog is useful in the simplest small applications as well as in large and complex ones. Due to its rich configuration abilities, you can use it in all your projects.
In the tutorial you will learn how to:
Head over to Better Stack and start shipping your NLog logs in minutes with our native .NET integration.
You will need:
To get started, you need to create a new project. You can do it in several ways in Visual Studio. The first one is to select Create a new project in Visual Studio start window.
If the Visual Studio IDE is already open, you can follow the path on the top menu bar *File > New > Project *and create a new project this way.
The last way we are going to show you is to use the shortcut:
Ctrl + Shift + N.
Before starting to work on the application, you need to install some dependency packages. Visual Studio provides multiple ways to use the NuGet Package Manager. In the tutorial we will use the Package Manager Console.
The first thing you have to do is to open the Package Manager Console. You can
do it using Tools > NuGet Package Manager > Package Manager Console.
Alternatively, you can sequentially press ALT, V, E, and O.
In the console that opens, run the following commands one by one.
The NLog.Extensions.Logging package is the root package of NLog.
For the application, we are going to use the simplest way to create a logger in
NLog — the LogManager class. It provides the GetCurrentClassLogger method to
get a unique logger per each class. Unlike the globally configured logger, the
logger per class helps you to easily capture the source of the log message.
The logger will use NLog's default log levels system. The system consists of the following 6 levels:
Let's start coding. Your Program.cs file should look like the code snippet
below.
NLog has rich configuration abilities and provides two ways to configure the logger: programmatically or via a configuration file. You can get advanced information about each of them in the documentation.
In this tutorial we're going to configure the logger using the nlog.config
file.
The first thing we need to do is to create a nlog.config file in the root of
the application. You can do it by pressing CRTL + SHIFT + A and entering the
nlog.config name in the opened window.
Then, you have to set the file's Copy to Output Directory property to Copy always. It can be done by right-clicking the file in the solution explorer and selecting the Properties option.
The config file must look like that:
Now, let's write a config. The config is written in the XML language. If you are not familiar with the XML language, you may dive in the great XML tutorial by w3school.
In our case the config consists of two parts: setting up the logging targets and setting up the logging rules.
We've decided to add 3 logging targets for the application: the colorized
console and 2 files. In the config file, each of them will be represented by a
separate XML element. Let's write the targets in the nlog.config file.
After that, we can create the rules. In NLog, each rule can be applied to multiple loggers and targets and must be written as a separate XML element.
The first rule enforces all loggers to write all logs generated by the application to the colorized console.
The second rule enforces all loggers to write logs with debug and higher
severity logs to the target named all_logs_file.
The third rule enforces all loggers to write warnings and higher severity logs
to the target named important_logs_file.
We're going to use 2 extra classes. To create a new file for a class, you can
press CRTL + SHIFT + A and select C# class in the window that opens.
The first file will contain a Person class. You can see its code in the
snippet below.
The second file will contain a Car class. You can see its code in the snippet
below.
With @ — destructuring operator we can break objects down into properties
with values.
To demonstrate how the logger works, we will log some simple messages, at least one of each level. It's a pretty straightforward task, because of the clear NLog API.
More specifically, NLog logger provides 6 methods for logging: Trace, Debug,
Info, Warn, Error, and Fatal.
Now, let's build and run the program. You can simply do this by pressing
CTRL + F5.
After the execution, your console's output should look like:
Now, let's check the logs written in the files. The files are located in the
C:\\logs.
The first, check your important.json file, it should look like.
The second, check your all.json file, it should look like.
Better Stack transforms your NLog data into interactive dashboards that reveal patterns, track error rates, and help you spot issues before they escalate.
Proper logging can greatly assist in the support and development of your application. This may seem like a daunting task, but NLog is a fast and configurable logging framework that greatly simplifies the task.
In the tutorial, you have configured your logging system with multiple logging targets for a .NET console application with NLog.
Now developing and maintaining your .NET applications will be much easier!
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.