.NET PROGRAMMING
This article demonstrates how to add file logging using NLog Framework.
![](https://cdn-images-1.medium.com/max/1200/1*jYvZuXd2aOkucaDM87kxLQ.jpeg)
Github Project
https://github.com/sukhsukhpinder/FileLoggingNlog
Step 1: Create an empty .NET core web api project inside visual studio.
Step 2: Install NLog.Web.AspNetCore from nuget manager
Step 3: Add a configuration file into the project. For example, “nlog.config”
Add the below code into the configuration for file logging. Path of the file is highlighted below
https://gist.github.com/ssukhpinder/54776c3e09b0f378303c52ce6854fbc0
Step 4: Define LogManager in Program.cs file as shown below
NOTE: Pass the configuration filename created in Step 3 in below line
var logger = NLogBuilder.ConfigureNLog(“nlog.config”).GetCurrentClassLogger();
NOTE: Pass the configuration filename created in Step 3 in below line
var logger = NLogBuilder.ConfigureNLog(“nlog.config”).GetCurrentClassLogger();
Step 5: Configure Logging in Program.cs
https://gist.github.com/ssukhpinder/278b463f1ffdb12e4b903a5a15cba639
As we are defining the log level as TRACE here, then we need to change the default log level in the appsettings.json file as well. Refer below image
Step 6: Use logging in any API controller as follows:
https://gist.github.com/ssukhpinder/298111524314f7d694804fcd0bdb1394
Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.