Log4net 以编程方式配置 adoAppender
我需要将 ado 附加程序添加到现有的 log4net 配置中。
我需要通过代码添加它。然而,我坚持添加常见的字符串参数,如消息,因为我无法正确定义布局属性:在 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于找到了如何在
IRawLayout
中转换PatternLayout
。为此,您必须使用 log4net.Layout.Layout2RawLayoutAdapter:
I've finally found how to transform the
PatternLayout
in anIRawLayout
.To do this, you have to use
log4net.Layout.Layout2RawLayoutAdapter
: