gpt4 book ai didi

c# - NLog.Config 从 IIS Express 文件夹而不是 Content Root 读取

转载 作者:行者123 更新时间:2023-11-30 17:27:40 27 4
gpt4 key购买 nike

我正在编写一个 ASP.NET Core 2.2 服务,它使用 Common.LoggingNLog

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

  "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 时,我得到了 The type initializer for '...' throw an exception

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

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

最佳答案

为什么不像 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

关于c# - NLog.Config 从 IIS Express 文件夹而不是 Content Root 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531140/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com