ASP.NET Tutorial: Logging in .NET Core with NLog

In this post, we go over the fundamentals of utilizing NLog for logging and walk through a step-by-step implementation with.NET Core 6.

Introduction of the Logging Process

Logging, to put it technically, is the process of keeping track of events and other data that the application generates while it is running. Logging is a useful tool for system administrators and developers to monitor application behavior and identify various problems. These records are log entries, which are commonly kept in a file or database and aid in system monitoring, analysis, and troubleshooting.

Benefits of Logging

Logging is an important thing in the software development life cycle, and there are several benefits to logging.

  • User Activity Tracking: Logs can capture different actions of users within the application, and that helps us understand the behavior of users. It also helps us in many ways to improve the system and for other analytical purposes.
  • Historical Data: Log data provides historical data and transactions of events, which helps us track changes over time and maintain important records of different events.
  • Monitoring and Performance Analysis: Logs help to monitor application performance, and developers or administrators can analyze data and user patterns that help us optimize performance and improve system efficiency.
  • Debugging and Troubleshooting: In the software development cycle, sometimes applications do not behave as expected. In that case, logging plays an important role in identifying what happened with the system by providing valuable information for developers or administrators.
  • Security: Logging also helps while investigating security incidents and events related to them, like when someone is doing suspicious activity and attempts to use an application. With the help of a log, the security team analyzes records and responds to them properly based on events.

NLog in .NET Core

  • NLog is a flexible and extensible logging framework for .NET Core Applications.
  • It is designed to be easily configured and customizable for a wide range of logging scenarios.
  • NLog supports various log targets, including files, databases, consoles, etc.
  • It captures the log messages in the application and helps us monitor and troubleshoot issues.

Step-By-Step Implementation

Step 1. Install the following NuGet package from the manager.

Step 2: Manually add the nlog.config file to the project and fill it with all the configuration (file location, database connection string, etc.) if you wish to store logs in a database.

The XML file known as the NLog configuration file specifies how the application’s logging feature operates. You can define and customize a number of options, including log targets, rules, and filters.

The following are the main components of the NLog configuration file.

  • ‘<nlog>’ – The root element configuration file is nlog; it contains the entire configuration of NLog.
  • ‘<targets>’ – The target element specifies where the log messages should be sent, such as to the database, console, or files.
  • ‘<rules>’ – The rule element defines the logging rule and determines which log message should be sent to specific targets based on the logger’s name and log levels.
  • ‘<loggger>’ – The logger element is used within the rules section, and it is used to define the rule for a specific logger.  In the logger element, name attribute specifies the logger’s name pattern to match, log level, etc.

Step 3. Weather Forecast class with required properties

Step 4. Weather Forecast controller with action method and logger injection in the constructor for logging purposes.

Step 5. Register required NLog services.
using NLog.Extensions.Logging;

Step 6. Run the application and execute the application endpoint with the help of Swagger, and in the log file, we can see our log entries with details. If any error occurs while registering the services in the program class, then the logger is also capturing the same as shown below.



Nlog additionally allows for modification in case we choose to format and alter the log or transfer it to cloud blob storage in accordance with needs.

ASP.NET 8 Hosting Recommendation

ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts