我需要在现有的Log4NETCONFIG中添加ADO APPENER。
我需要通过代码添加它。但是,由于无法正确定义布局属性,我一直在添加常见的字符串参数,如消息:在XML配置中,我们使用PatternLayout,但VIA代码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>

最佳答案

我终于找到了如何在PatternLayout中转换IRawLayout
为此,必须使用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);
    }

08-18 22:00