在我的 C# 中,我正在使用一个库(Outlook 加载项)。
我正在为 .NET 3.5 使用 NLog
问题是在我的开发机器上,有时 NLog 不会将日志写入文件。
我使用的是 NLog 版本 2.0.0.0,问题出现在 Windows 8.1 Pro
这是 App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File"
fileName="${specialfolder:folder=ApplicationData}\\AppName\\Logs\\log.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="30"
layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${level:uppercase=true} ${logger} - ${message} ${exception:format=tostring}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
在发行版中,目前我不包括文件 NLog.config,但也包括它,我没有看到任何变化
下面是出现问题的方法, 我应该在我的发行版中包含特殊的图书馆吗? (以 stdole.dll 为例?)
private void AddinModule_AddinStartupComplete(object sender, EventArgs e)
{
String aversion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
log.Debug("AddinStartupComplete called version: {0}", aversion);
}
最佳答案
好的,看来我找到了解决办法。
首先,我修改了App.config文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
然后我包含了一个文件 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="file" xsi:type="File"
fileName="${specialfolder:folder=ApplicationData}\\AppName\\Logs\\log.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="30"
layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${level:uppercase=true} ${logger} - ${message} ${exception:format=tostring}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>
我还指定在安装程序中包含文件 adxloader64.dll,尽管编译器会警告我该资源与指定的安装程序类型不兼容:x86。
注意:我正在使用 Add-in Express 7.4.x build设置
现在只有一点需要澄清:我应该检查这个设置是否也适用于 32 位 Windows 系统(我必须在 Windows 7 家庭上测试它)
关于c# - NLog 有时不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21093689/