Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net configuring programmatically an adoAppender

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>
like image 768
Franck LEVEQUE Avatar asked May 23 '11 09:05

Franck LEVEQUE


1 Answers

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);
    }
like image 141
Franck LEVEQUE Avatar answered Sep 22 '22 22:09

Franck LEVEQUE