无法让 NLog 抛出异常或写入日志(在我的机器上工作)

Can't get NLog to throw an exception or write a log (works on my machine)

我的工作站(Win10、IIS 10)上有一个 .Net webapi 运行ning,一切都很顺利。然而,当我将它推送到我们的生产环境(Win7、IIS 6)时,NLog 既不会写入日志也不会抛出异常。我的 NLog.config 在这里:

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true"
   >

  <targets async="true">
    <target
      name="logfile"
      xsi:type="File"
      layout="${date}|${level:uppercase=true}${message}"
      fileName="C:\inetpub\wwwroot\myapp\App_Data\webapi.log"
    />
  </targets>

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

无论我把fileName 属性设置成什么,它都不会在Win7盒子上写日志,也不会抛出异常。我会在 Windows 日志 -> 系统下的事件查看器中看到异常,对吗?

请注意,我没有目标 Win7 机器的管理权限,但有足够的提升权限将文件写入 wwwroot。

我错过了什么?异常或某种类型的错误会很好。

编辑:事实上它在 IIS10/Win10 上根本不起作用,尽管在 运行 中它在 Visual Studio 中工作正常(IIS Express?)。 Visual Studio 和 IIS 管理器都在 运行 "as administrator"。没有日志文件输出,没有异常,事件查看器中没有任何内容。 ???

https://github.com/NLog/NLog/wiki/Logging-Troubleshooting

上有 NLog 故障排除指南

When you get no log output from NLog, this can be because of the following reasons:

  • NLog cannot find the configuration file. This can happen when the NLog.config file is configured with Build Action = None or Copy to Output Directory = Do not copy in Visual Studio. Set Build Action = Content and "Copy to Output Directory = Copy if newer to fix this)
  • Is your configuration file valid XML?
  • Logging rules are incorrect or no rules are defined
  • Application tracing code is incorrect.
  • There is a runtime problem with the target (such as missing permissions)
  • Logs are written to a different location.

您也可以启用内部日志:https://github.com/NLog/NLog/wiki/Internal-logging

更新:如果您的内部日志不工作,则很难诊断问题。

您可以启用代码抛出异常:

LogManager.ThrowExceptions = true;
LogManager.ThrowConfigExceptions = true;