ASP.NET Tutorial : Implementation of NLog in.Application for the Internet Core

Leave a Comment

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.

using Microsoft.AspNetCore.Mvc;
using NLog;

namespace NLogApplication.Controllers
{
    public class ValuesController : ControllerBase
    {
        private readonly Logger _logger;

        public ValuesController()
        {
            _logger = LogManager.GetCurrentClassLogger();
        }
    }
}

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.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>

  <targets>
    <target xsi:type="File"
            name="file"
            layout="${verbose}"
            fileName="nlog.log"
            archiveFileName="log.{#}.log"
            archiveNumbering="Date"
            archiveEvery="Day"
            archiveDateFormat="yyyyMMdd" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>
</nlog>

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.

using Microsoft.AspNetCore.Mvc;
using NLog;

namespace NLogApplication.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly Logger _logger;

        public ValuesController()
        {
            _logger = LogManager.GetCurrentClassLogger();
        }

        [HttpGet]
        public IActionResult Get()
        {
            _logger.Info("This is the NLog Info.");
            _logger.Debug("This is the NLog Debug.");
            return Ok(DateTime.Now);
        }
    }
}

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.

  1. Right-click on your application in the solution explorer.
  2. Click on the Open Folder in file explorer.
  3. Go to the bin folder.
  4. Go to the Debug folder.
  5. Go to the .net 6.0 folder.
  6. Inside that, you have found the nlog.log file.

Open that file, and seems you see the logs inside that file as below. 


Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 7.0.10 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
 
https://hostforlifeasp.net/


Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment