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