gpt4 book ai didi

web-config - NLog 配置文件从 web.config 获取配置设置值

转载 作者:行者123 更新时间:2023-12-04 01:31:09 26 4
gpt4 key购买 nike

有没有一种方法可以从 <ApplicationSettings> 中获取值? NLog 布局变量中的 web.config 部分?

我已经在我的 web.config 中存储了 SMTP 详细信息,并且不想复制设置只是为了在我的 NLog.config 中使用。

理想情况下,我想做这样的事情:${aspnet-config:SmtpHostServer}然后从 web.config 中获取值

最佳答案

除了创建我自己的 LayoutRenderer 之外,我看不到任何明显的方法来做到这一点。 (见下文)。如果您要放入自己的程序集,请不要忘记将以下内容添加到您的 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));
}


}

关于web-config - NLog 配置文件从 web.config 获取配置设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7107499/

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