在asp.net core 2.1项目中没有加载NLog.config?

问题描述 投票:0回答:1

我有以下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"),似乎没有加载任何规则或目标?

enter image description here

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

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

c# nlog asp.net-core-2.1
1个回答
2
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.