behzad chizari
behzad chizari

Reputation: 146

ILogger<> doesn't cause NLog to log in File

I'm using NLog and I'm trying to log events using ILogger<> from Microsoft.Extensions.Logging qssembly. According to the documentations everything is all correct e.g nlog.config and Ilogger injection etc. but ILogger's methods don't work at all.

These are some pieces of code which works and I can see the result in file:

using NLog;
Logger logger = LogManager.GetLogger("foo");
logger.Info("Program started");

But I'm trying this(which doesn't work):

using Microsoft.Extensions.Logging;
public class UserController : ControllerBase
{
  public UserController(ILogger<UserController> logger)
  {
      _logger = logger;
  }

  public async Task<User> Create()
  {
      _logger.LogInformation("Create Method's called");
  }
}

This is nlog.config :

<?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"
  autoReload="true"
  internalLogLevel="info"
  internalLogFile="C:\temp\internal-nlog.log">
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>
  <targets async="true">
    <target xsi:type="File" 
            name="LogFile" 
            fileName="${basedir}\temp\nlog-${shortdate}.log" 
            layout="${longdate} | ${uppercase:${level}} | ${message} |     ${exception:format=tostring} | ${logger} | url: ${aspnet-request-url} |     action: ${aspnet-mvc-action}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="LogFile" />
  </rules>
</nlog>

This is inside of Program.cs:

    public static void Main(string[] args)
    {
        Logger loggerN = LogManager.GetLogger("foo");
        loggerN.Info("Program started");

        var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
        try
        {
            logger.Debug("init Main");
            CreateWebHostBuilder(args).Build().Run();
        }
        catch (Exception e)
        {
            logger.Error(e, "Program Shuted down");
            throw;
        }
        finally
        {
            LogManager.Shutdown();
        }
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseNLog()
            .ConfigureLogging(options => { options.ClearProviders(); })
            .UseStartup<Startup>();
}

here just two first lines of Main works.

Upvotes: 1

Views: 4342

Answers (1)

AlliterativeAlice
AlliterativeAlice

Reputation: 12577

Move UseNLog() to be after ConfigureLogging(options => { options.ClearProviders(); }). For example:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(options => { options.ClearProviders(); })
        .UseNLog();

If you call options.ClearProviders() after UseNLog(), it causes NLog to be removed as a logging provider.

Upvotes: 6

Related Questions