简体   繁体   English

.NET 4.5控制台应用程序中的NLog:未加载配置

[英]NLog in .NET 4.5 console app: not loading Configuration

I have some console apps I've written at work. 我有一些在工作中编写的控制台应用程序。 I'd like to get NLog into them but I am having trouble. 我想加入NLog,但遇到了麻烦。 When I inspect the 'logger' object, I see in it's 'Factory' property, that the configuration had targets=0, loggingrules=0, everything blank or unset. 当我检查“ logger”对象时,我在其“ Factory”属性中看到该配置的目标为= 0,loggingrules = 0,所有内容为空白或未设置。 So, it doesn't do ANYTHING.. doesn't drop an internal log file either... I have tried nLog.config NLog.config and nlog.config... to no avail. 因此,它不执行任何操作..也不会删除内部日志文件...我尝试了nLog.config NLog.config和nlog.config ...无济于事。 Tried ver 3 and 4 of NLog too... 也尝试了NLog的版本3和版本4 ...

Why would it not pick up the config? 为什么不选择配置?

I have: 我有:

  • NLog.config in the root with 'Content' for build action and 'Copy Always' set 根目录中的NLog.config具有“内容”以进行构建操作并设置了“始终复制”
  • Confirmed the NLog.config IS being copied to the bin 确认将NLog.config IS复制到垃圾箱 screenshot1

Here's the NLog.config: 这是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>

and finally (this doesn't error, but nothing is output since the config is blank): 最后(这不会出错,但是由于配置为空,因此不会输出任何内容):

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

Do your app.config have a NLog configSection? 您的app.config是否具有NLog configSection? Something like this: 像这样:

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

If even the internalLogger isn't working, you could debug the issue by setting the InternalLogger.LogWriter 如果甚至internalLogger无法正常工作,您也可以通过设置InternalLogger.LogWriter来调试问题

eg 例如

 // enable internal logging to a custom TextWriter
    InternalLogger.LogWriter = new StringWriter(); //e.g. TextWriter writer = File.CreateText("C:\\perl.txt")

I got the same issue on my end. 我最后遇到了同样的问题。 When you make change your fileName Path "c:\\temp\\testlog.log" to "c:/temp/testlog.log" then it will work. 当您将文件名路径“ c:\\ temp \\ testlog.log”更改为“ c:/temp/testlog.log”时,它将起作用。 Hope the below snipet help you to resolve the issue. 希望下面的内容能帮助您解决问题。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM