NLog.config не загружается в проект ядра 2.1 asp.net? - PullRequest
0 голосов
/ 30 августа 2018

У меня есть следующий 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>

И затем я следовал следующему учебнику Но я заменил

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 голосов
/ 31 августа 2018

internalLogLevel="on" должно быть internalLogLevel="Trace".

Вы можете добавить параметр throwConfigExceptions="true" рядом с throwExceptions="false". Это поможет вам отследить ошибки в NLog.config. На самом деле хорошая идея иметь throwConfigExceptions="true" даже в рабочих средах (в отличие от throwExceptions="true", который следует использовать только для модульного тестирования NLog)

...