log4net记录日志到数据库自定义字段

2020-09-03 15:37:52 浏览数 (1)

假设数据库中有如下自定义字段:

1、根据自定义字段定义日志信息对象

public class MessageLog

{

/// <summary>

/// 短信发送是否成功

/// </summary>

public int Success { get; set; }

/// <summary>

/// 发送号码(可以多个,逗号分隔)

/// </summary>

public string Mobiles { get; set; }

public string Message { get; set; }

/// <summary>

/// 发送内容

/// </summary>

public string Content { get; set; }

public override string ToString()

{

return this.Message;

}

}

2、根据自定义字段定义PatternConverter,以Content自定义字段为例,其它两个字段同

internal sealed class ContentPatternConverter : PatternLayoutConverter

{

protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)

{

var messageLog = loggingEvent.MessageObject as MessageLog;

if (messageLog != null)

{

writer.Write(messageLog.Content);

}

}

}

3、自定义PatternLayout

public class MessageLayout : PatternLayout

{

public MessageLayout()

{

this.AddConverter("Success", typeof(SuccessPatternConverter));

this.AddConverter("Mobiles", typeof(MobilesPatternConverter));

this.AddConverter("Content", typeof(ContentPatternConverter));

}

}

4、日志配置文件中添加数据库记录器并引用

<appender name="MySqlAdoNetAppender" type="log4net.Appender.AdoNetAppender">

<bufferSize value="1" />

<param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />

<param name="ConnectionString" value="Data Source=192.168.2.239;port=3306;Initial Catalog=log;user id=hhuser;password=123456;" />

<commandText value="INSERT INTO log4message(datetime,thread,level,logger,message,Exception,Success,Mobiles,Content) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @Success, @Mobiles, @Content)" />

<parameter>

<parameterName value="@log_date" />

<dbType value="DateTime" />

<layout type="log4net.Layout.RawTimeStampLayout" />

</parameter>

<parameter>

<parameterName value="@thread" />

<dbType value="String" />

<size value="255" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%thread" />

</layout>

</parameter>

<parameter>

<parameterName value="@log_level" />

<dbType value="String" />

<size value="50" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%level" />

</layout>

</parameter>

<parameter>

<parameterName value="@logger" />

<dbType value="String" />

<size value="255" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%logger" />

</layout>

</parameter>

<parameter>

<parameterName value="@message" />

<dbType value="String" />

<size value="4000" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%message" />

</layout>

</parameter>

<parameter>

<parameterName value="@exception" />

<dbType value="String" />

<size value="2000" />

<layout type="log4net.Layout.ExceptionLayout" />

</parameter>

<parameter>

<parameterName value="@Success" />

<dbType value="Int32" />

<layout type="BKMS.Service.Log.MessageLayout, BKMS.Service">

<conversionPattern value="%Success" />

</layout>

</parameter>

<parameter>

<parameterName value="@Mobiles" />

<dbType value="String" />

<size value="4000" />

<layout type="BKMS.Service.Log.MessageLayout, BKMS.Service">

<conversionPattern value="%Mobiles" />

</layout>

</parameter>

<parameter>

<parameterName value="@Content" />

<dbType value="String" />

<layout type="BKMS.Service.Log.MessageLayout, BKMS.Service">

<conversionPattern value="%Content" />

</layout>

</parameter>

<filter type="log4net.Filter.LevelRangeFilter">

<levelMin value="INFO" />

<levelMax value="INFO" />

</filter>

</appender>

0 人点赞