【问题标题】:ILogger debug messages not showing up in NLog targetsILogger 调试消息未显示在 NLog 目标中
【发布时间】:2020-07-18 05:05:32
【问题描述】:

我遇到了 ILogger 消息未发送到任何 NLog 目标的问题。 因此,例如,如果我有以下代码

public class TestConnectionController : ControllerBase
{
    private readonly ILogger<TestConnectionController> _logger;

    /// <summary>
    /// Initializes a new instance of the <see cref="TestConnectionController"/> class.
    /// </summary>
    public TestConnectionController(ILogger<TestConnectionController> logger)
    {
        _logger = logger;
    }

    public string TestLog()
    {
        _logger.LogDebug("This is an test"); <- Should show up in the logging targets
    }
}

它没有出现在任何预期的目标中。

我正在为 NLog 使用以下配置

"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"internalLogLevel": "Info",
"internalLogFile": "log/internal-nlog.log",
"extensions": [
  { "assembly": "NLog.Extensions.Logging" },
  { "assembly": "NLog.Web.AspNetCore" },
  { "assembly": "Microsoft.ApplicationInsights.NLogTarget" }
],
"targets": {
  "console": {
    "type": "Console"
  },
  "aiTarget": {
    "type": "ApplicationInsightsTarget"
  },
  "logFile": {
    "type": "File",
    "fileName": "log/nlog-${shortdate}.log"
  }
},
"rules": [
  {
    "logger": "*",
    "minLevel": "Info",
    "writeTo": "logFile"
  },
  {
    "logger": "*",
    "minLevel": "Debug",
    "writeTo": "console"
  },
  {
    "logger": "*",
    "minLevel": "Trace",
    "writeTo": "aiTarget"
  },
  {
    "logger": "Microsoft.*",
    "maxLevel": "Info",
    "final": "true"
  }
]
}

在 ConfigureServices 方法中通过以下方式加载到应用程序中。

public static void AddNLog(this IServiceCollection service, IConfiguration config)
    {
        var nLogConfig = new NLogLoggingConfiguration(config.GetSection("NLog"));

        service.AddLogging(builder =>
        {
            builder.ClearProviders();
            builder.AddNLog(nLogConfig);
        });
    }

我是否遗漏了一些明显的东西?

【问题讨论】:

    标签: c# nlog asp.net-core-3.1


    【解决方案1】:

    这可能是 Microsoft 日志记录扩展的默认过滤器。它默认过滤调试和跟踪日志。

    你(也)在你的 appsettings.json 中需要这样的东西:

    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Trace",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      }
    }
    

    这也是推荐的设置:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =>
          {
              webBuilder.UseStartup<Startup>();
          })
          .ConfigureLogging(logging =>
          {
              logging.ClearProviders();
              logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
          })
          .UseNLog();  // NLog
    

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多