首页 > 解决方案 > NLog 没有记录任何东西

问题描述

尽管我的项目中有以下文件,但 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 中发送协议消息,但它不执行任何操作。我总是会收到Messagebox的消息!为什么?当然,我在调试(任何 CPU)中运行应用程序,而不是在发布模式下

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>

标签: c#nlog

解决方案


修复了问题,但通过 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 吗?


推荐阅读