Configure NLog with .NET 6-8

Nlog

Install the following packages:

  1. NLog
  2. NLog.Extensions.Logging
  3. NLog.Web.AspNetCore

Add NLog to DI as following in .NET 6:


var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddControllersWithViews();
...

  
builder.Logging.ClearProviders();
builder.Host.UseNLog();

Add NLog to DI as following in .NET 8:


builder.Services.AddLogging(loggingBuilder =>
{
    loggingBuilder.ClearProviders();
    loggingBuilder.AddNLog();
});

Post a Comment

Previous Post Next Post