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

时间:2016-07-26 07:55:20

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

我的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)

项目结构:

Structure

更新(在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"

2 个答案:

答案 0 :(得分:2)

  

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

我很确定这不是问题所在。在注册目标时不评估路径。同样在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

,则会自动完成并进行错误检查

答案 1 :(得分:0)

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

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