Using NLog in MVC core

Tohid haghighi
2 min readMay 19, 2021

--

Logging is a very critical and essential part of any software. It helps us in the investigation of the essence of problems. ASP.NET Core has built-in support for logging APIs and is able to work with various logging providers. Using these built-in providers, we can send application logs to one or more destinations and also, we can plug in third-party logging frameworks, such as Serilog, Nlog, etc. In this article, we learn how to implement NLog with ASP.NET Core.

NLog is an open source and flexible framework that works with various .NET platforms including .NET Standard, Full framework (i.e., .NET Framework 4.7), Xamarin (Android and iOS), Windows phone, and UWP. NLog is easy to use and extend. Also, it provides more flexibility in term of configuration. We can also change the logging configuration on-the-fly. The target is used to store, display, and pass the log messages to the provided destination. NLog can write a log to one or more targets at the same time. NLog provides more than 30 targets that include file, event log, database, email, console, etc.

Following are the steps to configure NLog in ASP.NET Core application :

Step 1 - Install Nlog with Nuget package manager like below :

PM> Install-Package NLog

PM> Install-Package NLog.Web.AspNetCore

step 2 - Add Configuration :

Using “UseNLog” method, we can add NLog in a request pipeline as a dependency. To catch an error in program class, we can set up a logger class and after initialization of program class, we can safely shut down the log manager.

NLogBuilder.ConfigureNLog(“nlog.config”).GetCurrentClassLogger();

NLog.LogManager.Shutdown();

  • })
  • .UseNLog();

--

--

Tohid haghighi
Tohid haghighi

Written by Tohid haghighi

Full-Stack Developer | C# | .NET Core | Vuejs | TDD | Javascript

No responses yet