【问题标题】:NLog sometimes it doesn't write to fileNLog 有时它不会写入文件
【发布时间】:2014-01-13 14:29:12
【问题描述】:

在我的 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);
    }

【问题讨论】:

  • 您是否确定了未记录的程序或逻辑部分?可能错误发生在该逻辑之前,因此没有被记录
  • 是的,实际上我有一个日志行,在模块完成初始化时调用,并且调用此行不会对日志文件产生任何写入。
  • 如果您在该指令之前调用 log.Debug 它会被记录?
  • 不,我没有,但奇怪的是,当我在调试模式下启动托管应用程序(在本例中为 Outlook)时,日志文件会正确生成

标签: c# .net configuration nlog


【解决方案1】:

好的,看来我找到了解决办法。
首先,我修改了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 构建设置

现在只有一点需要澄清:我应该检查此设置是否也适用于 32 位 Windows 系统(我必须在 Windows 7 家庭中对其进行测试)

【讨论】:

    猜你喜欢
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多