[英]NLog is not logging anything
盡管我的項目中有以下文件,但Nlog不會協議任何內容:
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">
<!-- make sure to set 'Copy To Output Directory' option for this file -->
<!-- go to http://nlog-project.org/wiki/Configuration_file for more information -->
<targets>
<target name="logfile" xsi:type="File"
fileName="${basedir}/log/${shortdate}.log" />
</targets>
<rules>
<!--
Trace
Debug
Info
Warn
Error
Fatal
-->
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
以下代碼應在Debugmodus中提供協議消息,但它不會執行任何操作。 我總是會收到消息框的消息! 為什么? 當然,我是在Debug(任何CPU)上運行應用程序,而不是在Release模式下運行
LogLevel level = LogLevel.Debug;
Logger _logger = LogManager.GetCurrentClassLogger();
_logger.IsEnabled(level);
if(!_logger.IsDebugEnabled && !_logger.IsErrorEnabled && !_logger.IsInfoEnabled) {
MessageBox.Show("Protocolation not activated!" + Environment.NewLine + "Abbruch");
}else
_logger.Debug("Protocol item has been written into logfile!");
重新編輯:這是app.config看起來像:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
解決了問題,但使用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"> <!-- make sure to set 'Copy To Output Directory' option for this file --> <!-- go to http://nlog-project.org/wiki/Configuration_file for more information --> <targets> <target name="logfile" xsi:type="File" fileName="C:/Users/tklustig/Documents/Visual Studio 2017/Projects/C#/C#_GUI/Vokabelprogramm.git/log/${shortdate}.log" /> </targets> <rules> <!-- Trace Debug Info Warn Error Fatal --> <logger name="*" minlevel="Debug" writeTo="logfile" /> </rules> </nlog>
如果我這樣定義文件名:
fileName="${basedir}/log/${shortdate}.log"
不會有任何記錄! 為什么NLog不接受$ basedir? 這是無效的XML嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.