How to fix 'ILoggingBuilder' does not contain a definition for 'AddNLog'

By FoxLearn 5/14/2024 4:21:41 AM   119
This post shows you How to fix 'ILoggingBuilder' does not contain a definition for 'AddNLog' and no extension method 'AddNLog' accepting a first argument of type 'ILoggingBuilder'.

To resolve this error, you need to ensure that you have properly set up NLog in your project.

 

First, make sure you have installed the NLog package in your project. You can do this via NuGet Package Manager by right-clicking on your project, then select Manage Nuget Packages => Search 'NLog.Extensions.Logging' => Install it to your project.

nlog extensions logging

or by manually adding the package reference to your project file.

Install-Package NLog

Ensure that your NLog configuration is set up correctly. This typically involves creating an NLog.config file in your project and configuring the logging rules.

If you're using NLog with ASP.NET Core, ensure that you have added the necessary configurations to integrate NLog with the logging system.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args).ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddDebug();
        logging.AddNLog();
    }).ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
    });

Next, Double-check that you haven't misspelled any method names or forgotten to import the necessary namespaces. AddNLog() should be provided by the NLog.Extensions.Logging namespace.

using NLog.Extensions.Logging;

I hope so you can solve this issue by installing the Nuget Package called NLog.Extensions.Logging, it has a dependency of NLog.