问题描述
是否有一种方法可以从NLog布局变量中的web.config的<ApplicationSettings>
部分获取值?
Is there a method to get a value from the <ApplicationSettings>
section of a web.config within NLog layout variables?
我已经在我的web.config中存储了SMTP详细信息,并且不想复制仅在我的NLog.config中使用的设置.
I already store SMTP details within my web.config and don't want to duplicate the settings just to use within my NLog.config.
理想情况下,我想执行以下操作:${aspnet-config:SmtpHostServer}
然后从web.config获取值
Ideally I'd like to do something like: ${aspnet-config:SmtpHostServer}
which then fetches the value from the web.config
推荐答案
除了创建自己的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:
<extensions>
<add assembly="YOURASSEMBLYNAMEHERE" />
</extensions>
希望这对其他人有帮助
[LayoutRenderer("aspnet-config")]
public class AspNetConfigValueLayoutRenderer : LayoutRenderer
{
[DefaultParameter]
public string Variable
{
get;
set;
}
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (this.Variable == null)
{
return;
}
HttpContext context = HttpContext.Current;
if (context == null)
{
return;
}
builder.Append(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings[this.Variable], CultureInfo.InvariantCulture));
}
}
这篇关于NLog配置文件以从web.config获取配置设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!