我需要在现有的log4net配置中添加一个ado appender.
我需要通过代码添加它.但是我坚持添加常见的字符串参数,如消息,因为我无法正确定义布局属性:在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); }
<parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter>
解决方法
我终于找到了如何在IRawLayout中转换PatternLayout.
为此,您必须使用log4net.Layout.Layout2RawLayoutAdapter:
public static void AddStringParameterToAppender(this log4net.Appender.AdoNetAppender appender,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); }