Log4net 自定义字段到数据库(二)

这种方法比第一种方法麻烦些

Log4Net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <!-- Author:GaoBingBing-->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections> <log4net> <!--写入到数据库-->
<appender name="ADONetAppender_DbServer" type="log4net.Appender.AdoNetAppender">
<!--错误队列数据达到5个才持久化到数据库-->
<bufferSize value="" />
<param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
<param name="ConnectionString" value="Data Source=ip;initial catalog=test;user id=test;password=****;pooling=true;min pool size=1; max pool size=5;Connection Timeout=100;"/>
<commandText value="INSERT INTO xiao_log(Date,Thread,Level,Logger,Message,UserId,UserName)VALUES(@log_date, @thread, @log_level, @logger, @message,@userid,@username)"/>
<parameter>
<parameterName value="@log_date"/>
<dbType value="DateTime"/> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</layout>
<!-- <layout type="log4net.Layout.RawTimeStampLayout"/>-->
</parameter>
<parameter>
<parameterName value="@thread"/>
<dbType value="String"/>
<size value=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread"/>
</layout>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<size value=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level"/>
</layout>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<size value=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger"/>
</layout>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message"/>
</layout>
</parameter> <!--自定义字段--> <!--方法二-->
<parameter>
<parameterName value="@userid"/>
<dbType value="String" />
<size value="" />
<layout type="Log4Net.MyLayout">[注意了:这里的Log4Net是命名空间 ,MyLayOut是类名]
<conversionPattern value="%userid" />
</layout>
</parameter>
<parameter>
<parameterName value="@username"/>
<dbType value="String" />
<size value="" />
<layout type="Log4Net.MyLayout">
<conversionPattern value="%username" />
</layout>
</parameter> </appender>
<root>
<level value="ALL"/>
<appender-ref ref="ADONetAppender_DbServer"/> </root>
<!-- Specify the level for some specific categories -->
<logger name="iNotes">
<!-- <appender-ref ref="B" /> -->
<level value="ALL"/>
<appender-ref ref="ADONetAppender_DbServer"/> </logger> </log4net>
</configuration>
创建LogMessage.cs,类里面是所有的自定义字段属性

namespace Log4net
{
public class LogMessage
{
public string _userid; public string _username; public LogMessage() { } public LogMessage(string userId, string userName)
{
_userid = userId;
_username = userName;
} public string UserId
{
get { return _userid; }
set { _userid = value; }
} public string UserName
{
get { return _username; }
set { _username = value; }
} }
}
创建  ParemterContent.cs  添加参数转换器类,每个字段一个模式转化类

namespace Log4net
{
public class ParemterContent
{
internal sealed class UserIdParam : PatternLayoutConverter
{
// Methods
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
LogMessage content = loggingEvent.MessageObject as LogMessage;
if (content != null)
{
writer.Write(content.UserId);
}
}
} internal sealed class UserNameParam : PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
LogMessage content = loggingEvent.MessageObject as LogMessage;
if (content != null)
{
writer.Write(content.UserName);
}
}
} }
}
创建 MyLayout.cs  这个是不是很眼熟 <layout type="Log4Net.MyLayout">

namespace Log4net
{
internal class MyLayout : PatternLayout
{ public MyLayout()
{
this.AddConverter("username", typeof(ParemterContent.UserNameParam));
this.AddConverter("userid", typeof(ParemterContent.UserIdParam));
}
}
}
    log4net.ILog loger = log4net.LogManager.GetLogger("iNotes");
Log4net.LogMessage msg = new LogMessage("", "dajiahao!");
loger.Info(msg);
如果插入不成功需要检查  Ptoperties下面的AssemblyInfo.cs是不是加入了
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] Global.asax是不是加入了
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 如果缺少则需要加上。 如果还不成功 则检查Log4net.config配置文件,肯定xml中自定义属性有问题
上一篇:dell r710 安装ubuntu 12.04 server 启动后进入initramfs解决办法


下一篇:Ubuntu 11.04 NFS 配置