NLog.config 没有加载到 asp.net 核心 2.1 项目中?

NLog.config is not loaded in asp.net core 2.1 project?

我有以下NLog.Config

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="On" internalLogFile="c:\temp\nlog-internal.log">
  <targets>
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="f" />
  </rules>
</nlog>

然后我关注了以下tutorial 但是我替换了

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
        })
        .UseNLog()  // NLog: setup NLog for Dependency injection
        .Build();

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.SetMinimumLevel(LogLevel.Trace);
        })
        .UseNLog();

但是无论我做什么,都没有将任何内容写入指定文件或内部日志。

我也试过快速观看 NLog.Web.NLogBuilder.ConfigureNLog("nlog.config") 似乎没有加载规则或目标?

我还验证了 nlog.config 存在于构建目录 bin\Debug\netcoreapp2.1\ 中并且是正确的内容。

我也曾尝试将文件的绝对路径放在 NLog.Web.NLogBuilder.ConfigureNLog 中,但这没有帮助。

internalLogLevel="on" 应该是 internalLogLevel="Trace".

您可以在 throwExceptions="false" 旁边添加选项 throwConfigExceptions="true"。它将帮助您追踪 NLog.config 中的错误。即使在生产环境中使用 throwConfigExceptions="true" 实际上也是一个好主意(不像 throwExceptions="true",它应该只用于 NLog 的单元测试)