NLog 配置文件,用于从 web.config 获取配置设置值

发布于 2024-11-30 06:14:22 字数 266 浏览 0 评论 0原文

有没有一种方法可以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-12-07 06:14:22

除了创建我自己的 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));
    }


}

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>

Hope this helps someone else:

[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));
    }


}
醉城メ夜风 2024-12-07 06:14:22

更新答案

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-Renderer

NLog.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-Renderer

Original 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文