EP 1 : Logging in ASP .NET Core With NLog
Logging is the most important part of development, for figuring out the issues we need to add logging in our application especially if an issue is occurring at production.
We have multiple options available for logging but two options are the most popular, one of them is NLog and the other one is Serilog.
NLog is a free logging platform for .NET with rich log routing and management capabilities. It makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity.
Let’s see how can we install NLog and configure it for our ASP .NET Core Application
Installing NLog
Install these three packages from the Nuget package to enable NLog in your application. The appended versions are the latest as for now.
NLog (Latest 5.1.2)
NLog.Extensions.Logging (5.2.2)
NLog.Web.AspNetCore (5.2.2)
Configure NLog with appSettings.json
Add a NLog section in your appsetting with these lines.
These configurations are responsible for logging the details on console and file , as we have mentioned the file path.
If you don’t want to log the details on file you can exclude logfile from target section and second rule.
Update Program.cs
If you want to write your logs into file then add these lines in Program.cs
Now set up NLog Dependency Injection
Use Logs
There are 3 ways you can help me in growing:
Subscribe to my YouTube Channel
Subscribe to my Weekly .NET Newsletter of C#/.NET
Download my eBook at Gum Road which contains 30+ .NET Tips