NLog 未在所有级别上记录日志

2024-01-02

ASPNET Core 2.0 与最新的 Nlog。

所有配置文件均正确加载。

我的配置文件很简单,我只是希望它记录所有内容。

<?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="Warn"
      internalLogFile="C:\wwwLogs\nlog.log">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <targets>
    <target xsi:type="File" name="allfile" fileName="C:\wwwLogs\${shortdate}.log"
            maxArchiveFiles="90"
            archiveNumbering="DateAndSequence"
            archiveAboveSize="250000"
            archiveFileName="archive/log.{#######}.log"
            archiveEvery="Day"
            concurrentWrites="true"
            layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
  </targets>

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

我可以在 nlog 的跟踪日志中看到它正在将所有级别设置为正确的输出。

2017-11-01 14:21:26.3017 Trace Opening C:\wwwLogs\2017-11-01.log with allowFileSharedWriting=False
2017-11-01 14:21:28.5859 Debug Targets for TimeSlotApprovalService by level:
2017-11-01 14:21:28.5859 Debug Trace => allfile
2017-11-01 14:21:28.5859 Debug Debug => allfile
2017-11-01 14:21:28.5859 Debug Info => allfile
2017-11-01 14:21:28.5859 Debug Warn => allfile
2017-11-01 14:21:28.5859 Debug Error => allfile
2017-11-01 14:21:28.5859 Debug Fatal => allfile

在我的应用程序中,当我调用它时

_logger.LogDebug(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogError(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogCritical(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogWarning(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogTrace(JsonConvert.SerializeObject(rankedTimeSlots, Formatting.Indented));

那么日志文件只记录这些

2017-11-01 14:44:48.2570|TimeSlotApprovalService|**ERROR**|[json...

2017-11-01 14:44:48.2570|TimeSlotApprovalService|**FATAL**|[json...

2017-11-01 14:44:48.2570|TimeSlotApprovalService|**WARN**|[json...

剩下的人在哪里??跟踪和调试??信息?


ASP.NET Core 及其日志系统Microsoft.Extensions.Logging使用中央配置。无论附加的日志记录提供程序如何,此配置都适用。因此,如果您使用 NLog 作为日志记录提供程序,您仍然需要配置日志记录基础设施。

默认情况下,Web 主机构建器将自动使用appsettings.json配置日志记录详细程度。在默认模板中,配置如下所示:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

所以默认的最小日志级别是Warning。因此,即使您将 NLog 提供程序配置为以任何日志记录级别进行日志记录,它实际上也不会收到低于以下级别的日志记录指令:Warning来自日志系统。

因此,您必须调整那里的配置才能更改它。将其设置为Trace它应该记录一切。

请注意,您仍应考虑使用此处的配置作为应记录哪些日志级别的事实来源。因此,只需保留您的 NLog 配置来记录它得到的任何内容,然后调整您的appsettings.json匹配您想要实际记录的任何内容,具体取决于当前环境(您可以创建类似的文件appsettings.Development.json and appsettings.Production.json创建特定于环境的配置)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NLog 未在所有级别上记录日志 的相关文章

随机推荐