首页 > 解决方案 > NLog 致命消息从未记录?

问题描述

我在 C# 控制台应用程序中使用 NLog 4.7.10。我的信息、错误和警告消息按预期出现在我的日志文件中,但致命消息永远不会出现。我的代码如下所示:

NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

logger.Fatal("This never appears in the log file!");
logger.Info("This info message is logged in the log file.");
logger.Error("This error message is logged in the log file.");
logger.Warn("This warning message is logged in the log file.");

我认为我的 nlog.config 中没有任何内容会导致问题:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Warn"
      internalLogFile="d:\tmp\log.txt">

    <variable name="defaultLayout" value="${date:format=dd MMM yyyy HH\:mm\:ss} [${level}]: ${message:withException=true}" />

    <targets>
        <target name="logfile" xsi:type="File"
            fileName="${basedir}/logs/foo.log"
            archiveOldFileOnStartup="true"
            layout="${defaultLayout}"
            archiveFileName = "${basedir}/logs/archives/foo ${date:format=yyyy-MM-dd HHmmss}.log"
            archiveNumbering = "Rolling" />
        <target name="console" xsi:type="ColoredConsole" layout="${defaultLayout}" />
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="logfile" />
    </rules>
</nlog>

什么可以过滤掉致命消息?

标签: nlog

解决方案


我发现了这个问题。我们的一个函数,其目的是设置日志文件路径(给定一个数据库值)有一个副作用。它还设置了最大日志级别。在这种情况下,它将最大日志级别设置为错误。

抱歉打扰。


推荐阅读