NLog.config是否未在asp.net core 2.1项目中加载?

时间:2018-08-30 15:28:25

标签: c# nlog asp.net-core-2.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

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

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

1 个答案:

答案 0 :(得分:2)

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

您可以在throwConfigExceptions="true"旁边添加选项throwExceptions="false"。这将帮助您在NLog.config中查找错误。即使在生产环境中也拥有throwConfigExceptions="true"实际上是一个好主意(与throwExceptions="true"不同,ROOT- --lsapp ---app ---bootstrap ---config ---database ---resources ---routes ---storage ... --public_html ---css ---fonts ---js ---vendor ---index.php ... 仅应用于NLog的单元测试)