1 using log4net.Core; 2 using log4net.Layout.Pattern; 3 using System.IO; 4 using System.Reflection; 5 namespace LogComponent 6 { 7 class MessagePatternConverter : PatternLayoutConverter 8 { Convert(TextWriter writer, LoggingEvent loggingEvent) 10 { 11 if (Option != null) 12 { WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent)); 15 } { WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties()); 20 } 21 } 通过反射获取传入的日志对象的某个属性的值 LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent) 28 { 29 object propertyValue = string.Empty; 30 PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property); 31 if (propertyInfo != null) 32 propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null); 33 return propertyValue; 34 } 35 } 36 }
View CodeMyLayout.cs
1 using log4net.Layout; 2 namespace LogComponent 3 { 4 class MyLayout : PatternLayout 5 { 6 public MyLayout() 7 { , typeof(MessagePatternConverter)); 9 } 10 } 11 }
View Code其实看到这里,最重要的并不是代码了,核心部分Log4net都帮我们写好了,关键在于你的配置,下面是log4net.config的内容。拿到你的web项目里是一样用的。但是不要忘了在你的项目中引用nuget:log4net哟。
log4net.config如下:在其中主要配置了log入库的参数和sql语句,当然还有sql连接。注释已经很详细了
View Code
这样一来,你的配置就完成了,你可以直接测试插入的情况:
三. 把Log信息可视化我的UI使用的是Datatables.js,弹出框是layer,日期组件好像是layDate,下拉框是修改样式后的select2。UI代码是我自己的一个框架里的,内容太多就不贴出来了,你只需要和以前一样,把数据从库里查出来,绑定给任意你喜欢的数据表格上。由于单页面的日志系统没有什么复杂操作,就用个sqlHelper查一下就算了,代码和条件拼接如下
xxxDal 2 { 3 private SqlHelper _sqlHelper = new SqlHelper(); 获取xxx的日志 List<LogModel> GetxxxLog(SM_LogModel model) 11 { 12 StringBuilder sql = new StringBuilder(); 13 List<SqlParameter> sqlParameters = new List<SqlParameter>(); 14 StringBuilder sqlWhere = new StringBuilder(); 15 if (!string.IsNullOrWhiteSpace(model.LogStartTime)) 16 { , model.LogStartTime)); ); 19 } 20 if (!string.IsNullOrWhiteSpace(model.LogEndTime)) 21 { , model.LogEndTime)); ); 24 } 25 if (!string.IsNullOrWhiteSpace(model.LogLevel)) 26 { , model.LogLevel)); ); 29 } 30 if (!string.IsNullOrWhiteSpace(model.LogModule)) 31 { , model.LogModule)); ); 34 } 35 sql.AppendFormat(@" 36 WITH t AS ( SELECT ROW_NUMBER() OVER ( ORDER BY id DESC ) AS IndexNum , 37 [Id] , 38 CONVERT(VARCHAR, [LogDate], 21) AS [LogDate] , 39 [UserName] , 40 SUBSTRING([Description], 0, 150) AS [Description] , 41 SUBSTRING([LogMsg], 0, 200) AS [LogMsg] , 42 [LogLevel] , 43 [LogModule] 44 FROM [LogSystem].[dbo].[xxxLog] h 45 WHERE 1 = 1 46 {0} 47 ) 48 SELECT * 49 FROM t 50 WHERE IndexNum > @startIndex , sqlWhere); , model.Start)); , model.Start + model.Length)); 54 55 DataTable dt = _sqlHelper.ExecuteDataTable(sql.ToString(), sqlParameters.ToArray()); 56 return DataTableTools<LogModel>.DataTableToList(dt); 57 } GetxxxLogTotalCount(SM_LogModel model) 60 { 61 StringBuilder sql = new StringBuilder(); List<SqlParameter> sqlParameters = new List<SqlParameter>(); 62 sql.Append(@" 63 SELECT COUNT(*) ); 65 if (!string.IsNullOrWhiteSpace(model.LogStartTime)) 66 { , model.LogStartTime)); ); 69 } 70 if (!string.IsNullOrWhiteSpace(model.LogEndTime)) 71 { , model.LogEndTime)); ); 74 } 75 if (!string.IsNullOrWhiteSpace(model.LogLevel)) 76 { , model.LogLevel)); ); 79 } 80 if (!string.IsNullOrWhiteSpace(model.LogModule)) 81 { , model.LogModule)); ); 84 } 85 return _sqlHelper.ExecuteScalar<int>(sql.ToString(), sqlParameters.ToArray()); 86 } 87 88 [HttpPost] 89 public LogModel GetxxxxSignelLog(int id) 90 { SELECT [Id] , 93 CONVERT(VARCHAR(30), [LogDate], 21) AS [LogDate] , 94 [UserName] , 95 [Description] , 96 [LogMsg] , 97 [LogLevel] , 98 [LogModule] , 99 [Id] IndexNum 100 FROM [LogSystem].[dbo].[xxxxLog] h ; , id)); 103 return DataTableTools<LogModel>.DataRowToModel(row); 104 } 105 }
四. 写在最后