NLog with ILogger in .Net 6.0 Web API
There are several .Net things that are essential, but, they keep flying under the radar. One of those things is logging.
A few years ago, I was working for a real estate company in UK, remotely from India, of course. We had issues with figuring out where int he 250 step process was a simple process crashing. I had to spend hours and hours debugging the thing.
Then, I ended up building a simple logging library of my own. and, we started tracking the issues much faster.
I forgot to use ILogger. Now, it’s time to look at NLog, one of the many logging libraries available for .Net.
As always, the code is here.
CSharpForStudents/WebApiDotNet6/LoggingDotNet6 at main · Jay-study-nildana/CSharpForStudents
repository which contains some essential C sharp, dot net, dot net core and related .NET stuff …
My lifelong policy is do more with the absolute minimum. I chose NLog because the library has fantastic documentation. It took me only a few minutes to understand how to get the library working with .Net Web API. As a bonus, the documentation was updated to .Net 6.0.
Thank you NLog guys.
Further, Remember Microsoft already has the excellent ILogger interface. The whole point of using a provider such as NLog, is so that, you can do extra things, like writing to files.
Microsoft does not provide any providers that write to file. However, they do have providers that write to Azure services.
In this demo, I am only looking at writing to a file. I am confident, NLog can be used to write to wherever you want — online service like Azure Application Insights or a local database like SQLite or an online Azure SQL Server. Really, the options are endless.
Setting It Up
You know, by far the most confusing thing about using NLog was the exact Nuget Package to use. It’s probalby just me. Then again, this is my medium publication where I write about feelings and thoughts and despair.
Eventually, I got the right package.
<PackageReference Include="NLog.Web.AspNetCore" Version="4.14.0" />
Logging happens at a higher level. I mean, you start logging even before your run your web api.
So, your Program.cs looks something like this.
I want to point out this specific line.
This helps me use the Logger outside the controller. For example, like this.
Now, at this stage, you need to understand that we are banking on dependency injection to kick in. You will be doing this in the API Controller of course.
New to DI? Try one of my DI posts.
Simple Dependency Injection in .Net 6.0 Web API
Aha! There is always something intimidating about Dependency Injection. I distinctly remember a couple of years ago…
In my example, I am logging directly from within the controller. I also end up logging outside the controller. This is useful because, ultimately, you want your controller to look sleek and simple. You want to put all your actual code outside the controller.
I mean, you are using Test Driven Development write?
Log Output and Files
In the current example, I am writing all the logs to a simple file. You see, NLog uses a configuration file. It dictates where the logs get written into.
In the above configuration, the log files will be located in ‘C:\temp’ folder.
If all goes well, you will see output which looks something like this.
And, that’s all there is to it.
It would be super cool, if I could get this working with Azure Application Insights or push it to some kind of an event service build alerts and all that.
You should do that. If I end up doing it, I will definitely post it here.