c# - NLog 有时不写入文件

标签 c# .net configuration nlog

在我的 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/

相关文章:

url - 通过URL运行自动化脚本

c# - 当列表可以附加其他任务时等待 Task.WhenAny(List<T>) 的适当模式

C# Ionic.Zip 进度条作为后台 worker

持续交付中的配置信息

java - 多个 Java 服务器和批处理程序 - XML 配置噩梦

c# - 系统无效操作异常: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid

c# - 如何获取基类的泛型类型参数?

c# - 从 fieldinfo 获取通用值会引发异常 : "Late bounds operation cannot be performed..."

c# - Epplus:如何将 Excel 样式库中的样式应用到 ExcelRange

c# - 从 .net core 2.0 中动态加载的程序集调试代码