c# - 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>
解决方案
修复了问题,但通过 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 吗?
推荐阅读
- go - 如果我不指定 GOPATH,去哪里下载我的依赖项?
- bash - 需要在 groovy Jenkins 脚本中运行 Bash 命令
- c++ - C++ 中的浮点数(float 和 double)
- python - Keras 中 RNN/LSTM 的简单时间序列问题
- html - 手机版SHOPIFY图片重叠成文字
- firebase - Firebase Analytics 移动通过服务器
- python - RegEx 接受字符串、# 和数字
- docker - 在 docker config.json 中添加信息
- codeceptjs - Codeceptjs 中的方法:如果元素不可见 -> 向下滑动
- vtk - 尝试将阴影投射到网格平面上的 PyVista 错误