ASP.NET MVC - 未找到 NLog 文件目标

ASP.NET MVC - NLog file target not found

我对 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"

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

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

Update2: I found the solution. The problem was that I have the backslashes instead of forward slashes

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

问题是 XML 缺少 <targets>:

<?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

,您将获得自动完成和错误检查功能