2011-05-23 71 views
6

我需要將ado appender添加到現有的log4net配置中。Log4net以編程方式配置adoAppender

我需要通過代碼添加它。然而,我堅持添加像消息一樣的公共字符串參數,因爲我無法正確定義佈局屬性:在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> 

回答

17

我終於找到了如何在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); 
    } 
+0

小錯字,應該是 「Layout2RawLayoutAdapter」(而不是*行*)。 – mlangsworth 2012-11-07 03:17:43