| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Logging in .NET with NLog
Feb 18, 2023
3 min read

Logging in .NET with NLog

Sponsor this Newsletter

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 ( When article is being published)

  • 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.

Logging in ASP .NET Core With NLog

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

Logging in ASP .NET Core With NLog

Now set up NLog Dependency Injection

We can set up DI like this :

Logging in ASP .NET Core With NLog

Use Logs

In the controller we can use log by injecting ILogger :

Logging in ASP .NET Core With NLog

This article was originally published at https://mwaseemzakir.substack.com/ on Feb 18, 2023 .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.