c# - NLog in .NET 4.5 控制台应用程序 : not loading Configuration

标签 c# console-application nlog

我有一些在工作中编写的控制台应用程序。我想让 NLog 加入其中,但我遇到了麻烦。 当我检查“logger”对象时,我在它的“Factory”属性中看到配置有 targets=0,loggingrules=0,一切都是空白或未设置。 所以,它没有做任何事情.. 也不会删除内部日志文件...我已经尝试过 nLog.config NLog.config 和 nlog.config... 无济于事。也尝试了 NLog 的第 3 版和第 4 版...

为什么它不获取配置?

我有:

  • 根目录中的 NLog.config,其中包含构建操作的“内容”和“始终复制”设置
  • 确认 NLog.config 正在复制到 bin screenshot1

这是 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"
      autoReload="true"
      throwConfigExceptions="true"
      throwExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="c:\temp\NlogInternal.log"
      internalLogToConsole="true"
      internalLogToConsoleError="true"
      internalLogToTrace="true">
  <targets>
    <target xsi:type="Console" name="debugConsole" layout="${message} "/>
    <target xsi:type="File" name="debugFile" createDirs="true" fileName="c:\temp\testlog.log" layout="${longdate} ${uppercase:${level}} ${message}"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="debugConsole"/>
    <logger name="*" minlevel="Trace" writeTo="debugFile"/>    
  </rules>
</nlog>

最后(这不会出错,但由于配置为空,因此不会输出任何内容):

private static Logger logger = LogManager.GetCurrentClassLogger();
logger.Info("ConsoleApp test log...");

最佳答案

你的 app.config 有 NLog configSection 吗? 像这样:

<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <nlog>
  </nlog>
</configuration>

关于c# - NLog in .NET 4.5 控制台应用程序 : not loading Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024903/

相关文章:

c# - 在循环中更新文本的某些值

c++ - Win32 控制台程序中的清理代码

c# - 为什么 Main 在旋转新线程时等待,但任务却不是这样

c# - 将 nlog 中的记录添加到 dataType = date 的字段

c# - CaSTLe ILogger 和延迟加载 Func<string>

c# - Bouncy CaSTLe API 文档在哪里?

c# - Visual Studio 2010 中是否有可用的 ToString() 生成器?

c# - 如何更新页眉和页脚中的字段,而不仅仅是主文档?

c# - 索引格式每天更改为每周

c# - 字符串操作跟不上套接字