NLog is a free and open-source library. If you install the NLog package in your application, you are not required to perform any license-related actions.
The logging foundation for.Net or.Net Core is NLog.
NLog will assist you in troubleshooting the application error and resolving your issue.
How do you incorporate the NLog package into your application?
There are two methods for installing the NLog package.
Command
Manually launch the Nuget package manager, navigate to NLog, and install it.
Command
Install the NLog.Web.AspNetCore package.
Install the NLog manually
Open the project at the location where you wish to use NLog.
Right-click the solution and then select Manage Nuget Packages.
Enter NLog in the search box.Web.AspNetCore.
Once it is installed in your system, you can verify it on the below screenshot.
NLog provides different logging levels. The following log levels are supported:
- Info
- Error
- Debug
- Trace
- Fail
- Fatal
Initialize the logger in your application.
Configuring the logger in your application.
You need to create the nlog.config file in your solution.
Once you have created the file, you need to right-click it and select its properties.
Change the build action to None and Copy to Output Directory to Copy always.
The below content would be required for the nlog.config file.
To demonstrate whether the logs are working or not, you need to run the application.
Try to create one dummy API for that, as below.
Build the solution, and if you find any solution errors, please resolve those errors. Once your solution builds successfully, please run the application. Once your application will run successfully in your browser, open Swagger or Postman and try to hit the API.
Click on the Try it out button in the swagger documentation.
Click on the Execute button. Once the execution is done, look at the below screenshot.
For logging regarding verification, you need to follow the below steps.
- Right-click on your application in the solution explorer.
- Click on the Open Folder in file explorer.
- Go to the bin folder.
- Go to the Debug folder.
- Go to the .net 6.0 folder.
- Inside that, you have found the nlog.log file.
Open that file, and seems you see the logs inside that file as below.
0 comments:
Post a Comment