log4net保存到数据库系列五、新增数据库字段
2018-06-23 23:30:03来源:未知 阅读 ()
园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一、WebConfig中配置log4net
- 一、WebConfig中配置log4net
- 二、独立配置文件中配置log4net
- 三、代码中xml配置log4net
- 四、完整代码配置log4net
- 五、新增数据库字段
下面先一点一点来学习如果进行配置
1.下载,请参考log4net保存到数据库系列一:WebConfig中配置log4net
2.数据库脚本
CREATE TABLE [dbo].[Logs] ( [ID] INT IDENTITY (1, 1) NOT NULL, [Level] NVARCHAR (MAX) NULL, [Logger] NVARCHAR (MAX) NULL, [Message] NVARCHAR (MAX) NULL, [Date] DATETIME NOT NULL, [Exception] NVARCHAR (MAX) NULL, [ModName] NVARCHAR (MAX) NULL, [UserName] NVARCHAR (MAX) NULL, [Mac] NVARCHAR (MAX) NULL, [IP] NVARCHAR (MAX) NULL, [Url] NVARCHAR (MAX) NULL, [ActionName] NVARCHAR (MAX) NULL, [Name] NVARCHAR (MAX) NULL );
3.消息类
public class JimMessage : IRequiresSessionState { public JimMessage() { IP = Util.getLocalIP(); Mac = Util.getLocalMac(); Url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri; } public string IP { get; set; } public string Mac { get; set; } public string Url { get; set; } public string ModName { get; set; } public string ActionName { get; set; } public string Message { get; set; } public string UserName { get; set; } public string Name { get; set; } }
4.自定义字段解析类
public class MyLayout : PatternLayout { public MyLayout() { this.AddConverter("property", typeof(MyMessagePatternConverter));//指定自定义字段的解析类 } } public class MyMessagePatternConverter : PatternLayoutConverter//解析自定义属性,用反射获取属性名称 { protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent) { if (Option != null) { WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent)); } else { WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties()); } } /// <summary> /// 通过反射获取传入的日志对象的某个属性的值 /// </summary> /// <param name="property"></param> /// <returns></returns> private object LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent) { object propertyValue = string.Empty; PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property); if (propertyInfo != null) propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null); return propertyValue; } }
5..测试代码
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); AdoNetAppender adoNetAppender = new AdoNetAppender(); adoNetAppender.BufferSize = -1; adoNetAppender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; adoNetAppender.ConnectionString = "data source=.;initial catalog=test;persist security info=True;user id=sa;password=Jiehui@2016;MultipleActiveResultSets=True;"; adoNetAppender.CommandText = "INSERT INTO Logs ([Date],[Level],[Logger],[Message],[Exception],[UserName],[ModName],[Mac],[IP],[Url],[ActionName],[Name]) VALUES (@log_date, @log_level, @logger, @Message, @exception,@UserName,@ModName,@Mac,@IP,@Url,@ActionName,@Name)"; adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_date", DbType = System.Data.DbType.DateTime, Layout = new log4net.Layout.RawTimeStampLayout() }); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_level", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); PatternLayout layout = new MyLayout() {ConversionPattern = "%property{UserName}"}; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@UserName", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter( layout)}); layout = new MyLayout() { ConversionPattern = "%property{ModName}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@ModName", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Mac}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Mac", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{IP}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@IP", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Url}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Url", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{ActionName}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@ActionName", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Name}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Name", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Message}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Message", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); adoNetAppender.ActivateOptions(); BasicConfigurator.Configure(rep, adoNetAppender); ILog log = LogManager.GetLogger(rep.Name, "NoBufferingTest"); JimMessage message = new JimMessage(); message.Message = "审核成功"; message.UserName = "金朝钱"; message.Url = Request.Url.ToString(); message.ModName = "其他入库"; message.ActionName = "审核"; message.Name = "Bill20150505"; log.Debug(message);
6.测试成功
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MySql+Socket 完成数据库的增查Demo 2018-09-05
- 小程序问题集:保存失败:Error: ENOENT: no such file or di 2018-07-03
- 做测试做了一个百度地图api的例子保存读取坐标 2018-06-27
- 🈲Eclipse通过jdbc连接数据库制作简单登陆界面【 2018-06-24
- 用C#从数据库动态生成AdminLTE菜单的一种方法 2018-06-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash