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>
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With