log4net文件保存配置我就不说太多了,网上一大把的,数据库配置其实网上也有,只是我第一次按照网上的配置没有跑通,我就说下数据库配置需要注意的地方吧。
下面是一个log4net的数据库代码配置
public static void LoadADONetAppender() { log4net.Appender.AdoNetAppender adoAppender = new log4net.Appender.AdoNetAppender(); adoAppender.Name = "AdoNetAppender"; adoAppender.CommandType = CommandType.Text; int tempbuffsize = 1; int.TryParse(BufferSize, out tempbuffsize); adoAppender.BufferSize = tempbuffsize; adoAppender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; adoAppender.ConnectionString = _ConnectionString; adoAppender.CommandText = @"INSERT INTO [db_owner].[Log_Operation] (OpDate,OpType, UserIP, PageUrl, OpDesc,RecordID,UserName,UserID) " + "VALUES (@Date, @OpType, @UserIP, @PageUrl, @OpDesc,@RecordID,@UserName,@UserID)"; adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Date", DbType = System.Data.DbType.DateTime, Layout = new log4net.Layout.RawTimeStampLayout() }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@OpType", DbType = System.Data.DbType.String, Size = 10, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@UserIP", DbType = System.Data.DbType.String, Size = 128, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{UserIP}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@PageUrl", DbType = System.Data.DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{PageUrl}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@OpDesc", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@RecordID", DbType = System.Data.DbType.Int32, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{RecordID}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@UserName", DbType = System.Data.DbType.String, Size = 20, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{UserName}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@UserID", DbType = System.Data.DbType.String, Size = 10, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{UserID}")) }); adoAppender.ActivateOptions(); BasicConfigurator.Configure(adoAppender); }
其中private static readonly string _ConnectionString = Common.Config.ConntionString,就是读取webconfig配置里的数据库字符串,Common.Config.ConntionString是我自己写的的一个帮助类的一个属性,这里就不多说了,基本就是下面这样子:
<add key="SQLConnString" value="server=T-MOSSDB02\SQL2008_TEST;user id=xxx;password=xxx;database=xxxx;"/>
这里需要注意的地方就是Size一定要设值,且要和数据库对应字段的大小要一致,要不就报一错,具体名字不记得了。
还有就是我这里的那个RecordID为整型,如在对象里RecordID设成int?属性,那保存时一要要保证RecordID有值这样才能保存成功。我目前项目就碰到过这问题,由于我这项目不是我设计,开始也没有把log4net整入框架,所以每次保存时RecordID如为空,我都会附个默认值。
下面说下log4net扩展
一般情况下log4net所提供的信息不能满足项目记录需求,但将log4net简单扩展下就能完美解决这些问题了,如web项目,一般都会要记录请求的url地址和用户IP,用户名等一些信息。
1.首先建个自定义接口类(IWebLog),继承ILog
2.然后再建一个自定接口实现类(WebLogImpl)来实现我们的IWebLog接口,
3.然后再写一个操作帮助类供我们使用时更方便调用我们写的自定义接口实现类WebLogImpl
具体代码我就不贴了,我也是从网找的,网上资料很多,发现了一篇写得很详细:
http://www.cnblogs.com/longshizhong/archive/2009/11/25/1610452.html
最后,这篇文章就到这里了。