Logging is most important part of development , for figuring out the issues we need to add logging in our application especially if issue is occurring at production.
We have multiple options available for logging but two options are most popular , one of them is NLog and 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 Nuget package to enable NLog in your application. Appended versions are 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
Thank you so much for reading my Newsletter, see you in Next Week with New Episode.
Awsome.