NLog 记录器不会记录到发布版本中配置的日志文件
NLog logger doesn't log to the configured logfile in release build
我设置了配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog autoReload="true">
<targets>
<target name="file" type="File" fileName="${basedir}/log/${shortdate}.log" layout="${date:format=HH\:mm\:ss.fff}|${message}"/>
<target name="file_webs" type="File" fileName="${basedir}/log/${shortdate}_webs.log" layout="${date:format=HH\:mm\:ss.fff}|${message}"/>
</targets>
<rules>
<logger name="WebSocket.*" minlevel="Debug" writeTo="file_webs" final="true"/>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration>
记录器在每个 class 中加载,如下所示:
private static Logger logger = LogManager.GetCurrentClassLogger();
只要我不 运行 构建版本,日志记录就会定向到正确的文件。然后所有日志记录都在默认日志文件中完成。
可能是什么原因?
检查以下内容:
- 版本的输出目录中是否放置了正确的配置文件。也许那里有不同版本的配置文件或者它丢失了。
- 构建后还有其他事情运行吗?像混淆源代码的工具?如果是这样,这可能会弄乱构建中的 class 名称,配置无法重定向到正确的输出。在这种情况下,以这种方式显式加载 class 的记录器:
private static Logger logger = LogManager.GetLogger("WebSocket.*");
我设置了配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog autoReload="true">
<targets>
<target name="file" type="File" fileName="${basedir}/log/${shortdate}.log" layout="${date:format=HH\:mm\:ss.fff}|${message}"/>
<target name="file_webs" type="File" fileName="${basedir}/log/${shortdate}_webs.log" layout="${date:format=HH\:mm\:ss.fff}|${message}"/>
</targets>
<rules>
<logger name="WebSocket.*" minlevel="Debug" writeTo="file_webs" final="true"/>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration>
记录器在每个 class 中加载,如下所示:
private static Logger logger = LogManager.GetCurrentClassLogger();
只要我不 运行 构建版本,日志记录就会定向到正确的文件。然后所有日志记录都在默认日志文件中完成。 可能是什么原因?
检查以下内容:
- 版本的输出目录中是否放置了正确的配置文件。也许那里有不同版本的配置文件或者它丢失了。
- 构建后还有其他事情运行吗?像混淆源代码的工具?如果是这样,这可能会弄乱构建中的 class 名称,配置无法重定向到正确的输出。在这种情况下,以这种方式显式加载 class 的记录器:
private static Logger logger = LogManager.GetLogger("WebSocket.*");