【问题标题】:NLog ignores the config fileNLog 忽略配置文件
【发布时间】:2021-12-08 03:10:50
【问题描述】:

由于某种原因,NLog 忽略了配置文件。我有一个带有Build Action: ContentCopy to Output Directory: Copy alwaysNLog.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">

  <targets>
    <target name="default" xsi:type="Console" />
    <target name="dump" xsi:type="File" filename="./dump.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
      </layout>
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="DumpLogger" minlevel="Trace" writeTo="dump" />
    <logger name="*" minlevel="Trace" writeTo="dump" />
  </rules>
</nlog>

我有这个代码:

var logger = LogManager.GetLogger("DumpLogger");
logger.Trace("Try Trace"); // ignored
logger.Info("Try Info"); // writes to console

我还尝试在 GetLogger 调用之前添加显式 NLog 配置:

LogManager.LoadConfiguration(@"C:\Projects\ConsoleApplication1\bin\x64\Debug\NLog.config");

但我得到了相同的结果。

为什么 NLog 会忽略我的配置文件?

【问题讨论】:

  • 为什么你认为它被忽略了?因为找不到日志文件?
  • 不,因为minLevelTraceTrace 日志被忽略(仅输出调试及以上),因为DumpLogger 应该写入文件但日志转到安慰。该文件根本没有创建,但这不是唯一的症状。
  • Nlog文件名的默认文件名应该是&lt;ExecutableName&gt;.nlog,所以将NLog.Config作为MyApplication.exe.nlog复制到bin目录下。
  • 相同的行为。打印到控制台并忽略 Trace...

标签: c# logging nlog .net-framework-4.8


【解决方案1】:

经过一些更正,您提供的代码运行良好:

<?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">

  <targets>
    <target name="default" xsi:type="Console" />
    <target name="dump" xsi:type="File" filename="./dump.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <!-- Add something in the json entries -->
        <attribute name="timestamp" layout="${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss,fff}" />
        <attribute name="level" layout="${level}"/>
        <attribute name="message" layout="${message}" />
      </layout>
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="DumpLogger" minlevel="Trace" writeTo="dump" />
    <!-- Use default here instead of dump -->
    <logger name="*" minlevel="Trace" writeTo="default" />
  </rules>
</nlog>

还有:

using NLog;

namespace NLogTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var logger = LogManager.GetLogger("DumpLogger");
            logger.Trace("Try Trace");
            logger.Info("Try Info");

        }
    }
}

dump.log的内容:

{ "timestamp": "2021-10-21 11:26:34,672", "level": "Trace", "message": "Try Trace" }
{ "timestamp": "2021-10-21 11:26:34,697", "level": "Info", "message": "Try Info" }

控制台输出:

2021-10-21 13:13:27.8513|TRACE|DumpLogger|Try Trace
2021-10-21 13:13:27.8815|INFO|DumpLogger|Try Info

【讨论】:

  • 但它不应该自动加载配置文件吗?无论如何,它也不起作用(结果相同)
  • @nrofis 确实自动加载配置工作正常
  • 我不明白这个。这个配置仍然不适合我。我把它放在 bin 文件夹中的NLog.configConsoleApplication1.exe.nlog 中。什么都没有。
  • 关于默认值,这是我的意图,但这只是为了测试 - 根本没有控制台输出。
  • 注意文档说LogFactory instance for fluent interface,所以返回值是活动的LogFactory。所以这个答案应该更新以反映这一点。另请注意,NLog 将尝试从 several locations 自动加载 Nlog.config。
猜你喜欢
  • 1970-01-01
  • 2020-09-16
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2016-08-31
  • 1970-01-01
相关资源
最近更新 更多