【问题标题】:NLog is not writing debug events to the logNLog 没有将调试事件写入日志
【发布时间】:2021-02-25 11:02:02
【问题描述】:

我有以下 appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Trace",
      "Microsoft.Hosting.Lifetime": "Trace"
    }
  },
  "ConnectionStrings": {
    "dbDatabase": "Server=aaaa-aaaa;Database=fffff;uid=fffff;pwd=fffff;Trusted_Connection=True;"
  },
  "AllowedHosts": "*",
  "Jwt": {
    "Key": "wwwww",
    "Issuer": "Test.com"
  }
}

我的 Program.cs 看起来像这样

public class Program
    {
        public static void Main(string[] args)
        {
            var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

            logger.Debug("init main");
            CreateHostBuilder(args).Build().Run();
        }

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

我的 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.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file  -->
    <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxlevel="Trace" final="true" /> <!-- BlackHole without writeTo -->
    <logger name="System.Net.Http.*" maxlevel="Trace" final="true" /> <!-- BlackHole without writeTo -->

    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

但是,当我从我的代码中调用 _logger.LogDebug("test"); 时,它不会被写入日志。

如果我打电话给_logger.LogError("test");,它确实会被写入。

发生了什么事。这显然是一个日志级别问题,但我已将所有设置设置为 Trace!

【问题讨论】:

  • 您是否检查过其他 appsettings 文件,例如 appsettings.Development.json?
  • 没有 development.json 只有标准的 appsettings.json 和 appsetting.release.json。我在调试模式下运行,所以不会使用,

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


【解决方案1】:

在 appsettings.json 中定义应记录的命名空间。

例如:

"Logging": {
  "LogLevel": {
     "MyNameSpace" : "Information"}}

【讨论】:

  • 如果没有列出特定的命名空间,肯定会使用默认值吗?我以前从来不需要这样做。
  • 可能在 nLog 配置中将 maxLevel 从 Trace 更改为 Debug。您的最低和最高等级相同。
  • 我已尝试将最大级别更改为错误,但仍然没有更改。
【解决方案2】:

我重新启动了我的机器,突然间开始出现调试级别的日志!

不知道到底发生了什么。

【讨论】:

  • 很高兴它已得到解决。对于这种情况,我建议删除问题。如果我们无法重现它,那就太令人困惑了。我确定 NLog 在重新启动后不会做不同的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
相关资源
最近更新 更多