Log4net 以编程方式配置 adoAppender

发布于 2024-11-08 22:33:27 字数 1043 浏览 2 评论 0原文

我需要将 ado 附加程序添加到现有的 log4net 配置中。

我需要通过代码添加它。然而,我坚持添加常见的字符串参数,如消息,因为我无法正确定义布局属性:在 xml 配置中,我们使用 PatternLayout,但通过代码 PatternLayout 不是 IRowLayout,所以我在以下代码中遇到错误

    public static void AddStringParameterToAppender(this log4net.Appender.AdoNetAppender appender, string paramName, int size, string conversionPattern)
    {
        log4net.Appender.AdoNetAppenderParameter param = new log4net.Appender.AdoNetAppenderParameter();
        param.ParameterName = paramName;
        param.DbType = System.Data.DbType.String;
        param.Size = size;
        param.Layout = new log4net.Layout.PatternLayout(conversionPattern);
        appender.AddParameter(param);
    }

:有人知道如何在代码中添加字符串参数,类似于以下 xml 示例:

<parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
    </layout>
</parameter>

I need to add an ado appender to an existing log4net config.

I need to add it via code. However i'm stuck on adding common string parameters like message as I can't define the layout property correctly : In xml configuration, we use PatternLayout, but via code PatternLayout is not a IRowLayout so I have an error with the following code :

    public static void AddStringParameterToAppender(this log4net.Appender.AdoNetAppender appender, string paramName, int size, string conversionPattern)
    {
        log4net.Appender.AdoNetAppenderParameter param = new log4net.Appender.AdoNetAppenderParameter();
        param.ParameterName = paramName;
        param.DbType = System.Data.DbType.String;
        param.Size = size;
        param.Layout = new log4net.Layout.PatternLayout(conversionPattern);
        appender.AddParameter(param);
    }

Does someone know how to add a string parameter in code which is similar to the following xml sample :

<parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
    </layout>
</parameter>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

终止放荡 2024-11-15 22:33:27

我终于找到了如何在 IRawLayout 中转换 PatternLayout

为此,您必须使用 log4net.Layout.Layout2RawLayoutAdapter:

public static void AddStringParameterToAppender(this log4net.Appender.AdoNetAppender appender, string paramName, int size, string conversionPattern)
    {
        log4net.Appender.AdoNetAppenderParameter param = new log4net.Appender.AdoNetAppenderParameter();
        param.ParameterName = paramName;
        param.DbType = System.Data.DbType.String;
        param.Size = size;
        param.Layout = new log4net.Layout.Layout2RawLayoutAdapter(new log4net.Layout.PatternLayout(conversionPattern));
        appender.AddParameter(param);
    }

I've finally found how to transform the PatternLayout in an IRawLayout.

To do this, you have to use log4net.Layout.Layout2RawLayoutAdapter:

public static void AddStringParameterToAppender(this log4net.Appender.AdoNetAppender appender, string paramName, int size, string conversionPattern)
    {
        log4net.Appender.AdoNetAppenderParameter param = new log4net.Appender.AdoNetAppenderParameter();
        param.ParameterName = paramName;
        param.DbType = System.Data.DbType.String;
        param.Size = size;
        param.Layout = new log4net.Layout.Layout2RawLayoutAdapter(new log4net.Layout.PatternLayout(conversionPattern));
        appender.AddParameter(param);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文