NLog没有记录任何内容

时间:2018-11-28 13:35:27

标签: c# nlog

Nlog不会执行任何协议,尽管我的项目中包含以下文件:

enter image description here

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中使用protocoll消息,但是它什么也没做。我总是会收到消息框的消息!为什么?当然,我是在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>

1 个答案:

答案 0 :(得分:0)

已修复问题,但使用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吗?