从IIS Express文件夹而不是内容根目录中读取NLog.Config

时间:2019-02-05 09:24:39

标签: c# asp.net-core nlog appsettings

我正在编写一个ASP.NET Core 2.2服务,该服务使用 Common.Logging NLog

我在appsettings.json文件中使用NLog连接了Common.Logging:

  "LogConfiguration": {
    "factoryAdapter": {
      "type": "Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog45",
      "arguments": {
        "configType": "FILE",
        "configFile": "~/nlog.config"
      }
    }
  }

在Startup.cs中,我添加了:

    var logConfiguration = new LogConfiguration();
    Configuration.GetSection("LogConfiguration").Bind(logConfiguration);
    LogManager.Configure(logConfiguration);

当我开始服务时,代码出现在 logger.Info 中,I get a The type initializer for '...' threw an exception

对于NLog,这通常意味着未找到目标。我使用Procmon.exe搜索nlog.config的失败访问,发现尝试读取: C:\ Program Files \ IIS Express \〜\ nlog.config

如何让IIS Express在内容根目录下查找nlog.config?

1 个答案:

答案 0 :(得分:1)

为什么不按照PullRequest所示进行操作:

{
  "LogConfiguration": {
    "factoryAdapter": {
      "type": "Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog45",
      "arguments": {
        "configType": "INLINE",
      }
    }
  }
}

然后让NLog 4.5加载自己的LoggingConfiguration。

https://github.com/net-commons/common-logging/pull/176

您还可以从所需位置显式记录NLog.config:

https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading