【问题标题】:ASP.NET MVC - NLog file target not foundASP.NET MVC - 未找到 NLog 文件目标
【发布时间】:2016-07-27 02:45:23
【问题描述】:

我对 NLog 有疑问。 我在文件 Nlog.config 中配置了 NLog,如下所示:

<?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"
      throwExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="d:\temp\nlog-internal.log">

  <target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=dd}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />


  <rules> 
    <logger name="*" minlevel="Debug" writeTo="log" />

  </rules>
</nlog>

我得到一个找不到目标日志的错误。我真的不知道我能做什么,也许有人有类似的问题。

我正在使用 .NET 4.5。

Nlog.config 位于项目的主目录中。 我在目录中设置了完全访问权限。

我尝试将配置移动到 web.config 文件中,但仍然出现错误。

2016-07-26 09:17:25.6353 Warn Skipping unknown node: target
2016-07-26 09:17:25.6353 Trace ParseRulesElement
2016-07-26 09:17:25.6623 Error Error in Parsing Configuration File. Exception: NLog.NLogConfigurationException: Exception occurred when loading configuration from D:\Project\NLog.config ---> NLog.NLogConfigurationException: Target log not found.
   w NLog.Config.XmlLoggingConfiguration.ParseLoggerElement(NLogXmlElement loggerElement, IList`1 rulesCollection)
   w NLog.Config.XmlLoggingConfiguration.ParseRulesElement(NLogXmlElement rulesElement, IList`1 rulesCollection)
   w NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
   w NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   w NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)

项目结构:

更新(在 Michel A. 回答之后):我在 NLog.config 文件中也有属性

Build Action = Content
Copy to Output Directory = Copy always

Update2:我找到了解决方案。问题是我有反斜杠而不是正斜杠

语法错误:

fileName="${basedir}\logs\${date:format=dd}.log"

正确的语法:

fileName="${basedir}/logs/${date:format=dd}.log"

【问题讨论】:

  • 检查解决方案文件夹中的 /logs/ 文件夹,并检查当前用户是否具有读/写权限(或者是否存在)。在您当前的图片中,它不是解决方案的一部分,因此这可能是一个问题。
  • 我找到了解决方案。我做了一个更新。感谢您的回复:)

标签: c# asp.net asp.net-mvc nlog


【解决方案1】:

Update2:我找到了解决方案。问题是我有反斜杠而不是正斜杠

我很确定这不是问题所在。在注册目标时不会评估路径。在 Windows 上,斜线的种类也无关紧要。

问题在于 XML 缺少 &lt;targets&gt;:

<?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"
      throwExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="d:\temp\nlog-internal.log">

  <target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=dd}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />


  <rules> 
    <logger name="*" minlevel="Debug" writeTo="log" />

  </rules>
</nlog>

应该是:

<?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"
      throwExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="d:\temp\nlog-internal.log">

  <targets>
    <target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=dd}.log"
          layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="log" />

  </rules>
</nlog>

提示:如果您安装 NLog.Schema package,您将获得自动完成和错误检查

【讨论】:

  • 感谢您的回答。检查我的代码后,你也是对的
【解决方案2】:

您的 NLog 配置文件是否正在复制到您的 bin 文件夹中?

(在 Visual Studio 中检查属性文件)

【讨论】:

  • 我的属性是:构建操作 = 内容,复制到输出目录 = 始终复制
  • 如果你能在 bin 文件夹中看到它,那不是问题,对不起,我不知道你发生了什么
  • 我找到了解决方案。谢谢回复! :)
猜你喜欢
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多