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

3
我需要翻译如下内容:

我有关于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

更新2: 我找到了解决方案。问题是我使用反斜杠而不是正斜杠。

错误的语法:

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

正确的语法:

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

请检查您解决方案文件夹中的 /logs/ 文件夹,并检查当前用户是否具有对其的读写权限(或是否存在)。在您当前的情况下,它不是解决方案的一部分,因此可能会出现问题。 - Marco
我找到了解决方案。我进行了更新。感谢您的回复 :) - kamyk
3个回答

2
更新2:我找到了解决方案。问题在于我使用的是反斜杠而不是正斜杠。
我非常确定那不是问题所在。路径在注册目标时不会被评估。在Windows上,斜杠的类型也无关紧要。
问题在于XML缺少标签:
<?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包,您将获得自动完成和错误检查功能。


谢谢你的回答。检查了我的代码后,你说得对。 - kamyk

0
你的NLog配置文件是否被复制到了你的bin文件夹中?
(在Visual Studio中检查属性文件)

1
我的属性是:Build Action = content,Copy to Output Directory = Copy always。 - kamyk
如果你可以在bin文件夹中看到它,那就不是问题。抱歉,我不知道发生了什么事情。 - Michel Amorosa
我找到了解决方案。感谢您的回复! :) - kamyk

0
我正在使用 .Net Core 3.1,在编程时遇到了以下异常:*"NLogConfigurationException: Target 'ownFile-web' not found for logging rule: .",这是因为我的 nlog.config 文件名错误。正确的文件名应该是:"Nlog.Config"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接