NLog is a free, open-source, and easy-to-use logging framework that supports .Net Core and ASP.Net Core as well as the .Net Framework, Xamarin, and other Microsoft platforms. NLog is easy to configure and extend and comes with excellent support for log routing and management capabilities. This makes it a good choice when you must choose a logging framework that is fast and flexible.
In one of my earlier posts here, I covered the basics of NLog and how it can be used to log data to a file using .Net. In this article we will explore how we can take advantage of ASP.Net Core’s built-in support for dependency injection to use NLog as middleware.
Create an ASP.Net Core Web API project
If you’re running Visual Studio 2017, you can follow the steps outlined below to create an ASP.Net Core Web API project.
- Launch the Visual Studio IDE.
- Click on File > New > Project.
- Select “ASP.Net Core Web Application (.Net Core)” from the list of templates displayed.
- Specify a name for the project.
- Click OK to save the project.
- Select “API” in the “New .Net Core Web Application…” window.
- Select the version of ASP.Net Core you want to use.
- Uncheck the “Enable Docker Support” box and select “No Authentication” as we won’t be using either of these here.
- Click OK.
This will create a new ASP.Net Core Project in Visual Studio 2017. Next, install the following two packages to the project via the NuGet Package Manager in Visual Studio: