NLog 忽略配置文件
NLog ignores the config file
出于某种原因,NLog 忽略了配置文件。我有一个包含 Build Action: Content
和 Copy to Output Directory: Copy always
的 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">
<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 忽略我的配置文件?
经过一些修正,您提供的代码运行良好:
<?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
出于某种原因,NLog 忽略了配置文件。我有一个包含 Build Action: Content
和 Copy to Output Directory: Copy always
的 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">
<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 忽略我的配置文件?
经过一些修正,您提供的代码运行良好:
<?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