NLog 配置文件,用于从 web.config 获取配置设置值
有没有一种方法可以从 NLog 布局变量中 web.config 的
部分获取值?
我已经将 SMTP 详细信息存储在 web.config 中,并且不想复制这些设置只是为了在 NLog.config 中使用。
理想情况下,我想做类似的事情: ${aspnet-config:SmtpHostServer}
然后从 web.config 中获取值
Is there a method to get a value from the <ApplicationSettings>
section of a web.config within NLog layout variables?
I already store SMTP details within my web.config and don't want to duplicate the settings just to use within my NLog.config.
Ideally I'd like to do something like: ${aspnet-config:SmtpHostServer}
which then fetches the value from the web.config
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了创建我自己的 LayoutRenderer(见下文)之外,我看不出有任何明显的方法可以做到这一点。如果您要放入自己的程序集中,请不要忘记将以下内容添加到您的 NLog.Config 中:
希望这对其他人有帮助:
I couldn't see any obvious way to do this other than creating my own
LayoutRenderer
(see below). If you're putting into your own assembly don't forget to add the following into your NLog.Config:Hope this helps someone else:
更新答案
NLog版本。 4.6 在核心 NLog-nuget-package 中包含
${appsetting:SmtpHostServer}
。不再需要 NLog.Extended。另请参阅 https://github.com/nlog/NLog/wiki/AppSetting-布局渲染器NLog.Extensions.Logging 版本。 1.4 包含
${configsetting}
,允许从 appsettings.json 读取设置。另请参阅 https://github.com/NLog/NLog/wiki/ConfigSetting-布局渲染器原始答案
如今,无需自定义代码即可实现:
使用 NLog.Extended 并使用
${appsetting:SmtpHostServer}
。请参阅 ${appsetting} 的文档
请注意:这不是.NET Core / .NET 标准尚受支持。
Updated Answer
NLog ver. 4.6 includes
${appsetting:SmtpHostServer}
in the core NLog-nuget-package. No longer requires NLog.Extended. See also https://github.com/nlog/NLog/wiki/AppSetting-Layout-RendererNLog.Extensions.Logging ver. 1.4 includes
${configsetting}
that allows one to read settings from appsettings.json. See also https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-RendererOriginal Answer
Nowadays this is possible without custom code:
Use NLog.Extended and use
${appsetting:SmtpHostServer}
.See docs for ${appsetting}
Please note: this isn't supported in .NET Core / .NET standard yet.