c# – Log4net以编程方式配置adoAppender

前端之家收集整理的这篇文章主要介绍了c# – Log4net以编程方式配置adoAppender前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在现有的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);
    }

有人知道如何在代码添加字符串参数,类似于以下xml示例:

<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);
    }

猜你在找的C#相关文章