.net 日志系统解析(2)

其实看到这里,最重要的并不是代码了,核心部分Log4net都帮我们写好了,关键在于你的配置,下面是log4net.config的内容。拿到你的web项目里是一样用的。但是不要忘了在你的项目中引用nuget:log4net哟。

.net 日志系统解析

log4net.config如下:在其中主要配置了log入库的参数和sql语句,当然还有sql连接。注释已经很详细了

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <root > <level value="Debug"/> <appender-ref ref="ADONetAppender"/> </root> <logger> <level value="Debug"/> <appender-ref ref="ADONetAppender"/> </logger> <appender type="log4net.Appender.ADONetAppender,log4net"> <!--BufferSize为缓冲区大小,只有日志记录超value条才会一块写入到数据库--> <bufferSize value="1"/> <!--或写为<param value="1" />--> <!--引用--> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <!--连接数据库字符串--> <connectionString value="Data Source=115.29.54.31;Initial Catalog=LogSystem;uid=sa;pwd=sa.;MultipleActiveResultSets=True"/> <!--插入到表Log--> <commandText value="INSERT INTO HdPubLog ([LogDate],[LogMsg],[UserName],[Description],[LogLevel],[LogModule]) VALUES (@log_date,@LogMsg,@UserName,@Description,@LogLevel,@LogModule)"/> <parameter> <parameterName value="@log_date"/> <dbType value="DateTime"/> <layout type="log4net.Layout.RawTimeStampLayout"/> <!--获取log4net中提供的日志时间RawTimeStampLayout为默认的时间输出格式--> </parameter> <parameter> <parameterName value="@LogMsg"/> <dbType value="String"/> <size value="1510"/> <layout type="LogComponent.MyLayout, LogComponent"> <param value="%property{LogMsg}"/> </layout> </parameter> <parameter> <parameterName value="@UserName"/> <dbType value="String"/> <size value="50"/> <layout type="LogComponent.MyLayout, LogComponent"> <param value="%property{UserName}"/> </layout> </parameter> <parameter> <parameterName value="@Description"/> <dbType value="String"/> <size value="1510"/> <layout type="LogComponent.MyLayout, LogComponent"> <param value="%property{Description}"/> </layout> </parameter> <parameter> <parameterName value="@LogLevel"/> <dbType value="String"/> <size value="50"/> <layout type="LogComponent.MyLayout, LogComponent"> <param value="%property{LogLevel}"/> </layout> </parameter> <parameter> <parameterName value="@LogModule"/> <dbType value="String"/> <size value="50"/> <layout type="LogComponent.MyLayout, LogComponent"> <param value="%property{LogModule}"/> </layout> </parameter> </appender> </log4net> </configuration>

这样一来,你的配置就完成了,你可以直接测试插入的情况:

.net 日志系统解析

三.   把Log信息可视化

我的UI使用的是Datatables.js,弹出框是layer,日期组件好像是layDate,下拉框是修改样式后的select2。UI代码是我自己的一个框架里的,内容太多就不贴出来了,你只需要和以前一样,把数据从库里查出来,绑定给任意你喜欢的数据表格上。由于单页面的日志系统没有什么复杂操作,就用个sqlHelper查一下就算了,代码和条件拼接如下

public class xxxDal { private SqlHelper _sqlHelper = new SqlHelper(); /// <summary> /// 获取xxx的日志 /// </summary> /// <param></param> /// <returns></returns> public List<LogModel> GetxxxLog(SM_LogModel model) { StringBuilder sql = new StringBuilder(); List<SqlParameter> sqlParameters = new List<SqlParameter>(); StringBuilder sqlWhere = new StringBuilder(); if (!string.IsNullOrWhiteSpace(model.LogStartTime)) { sqlParameters.Add(new SqlParameter("@LogStartTime", model.LogStartTime)); sqlWhere.Append(@" AND h.LogDate > @LogStartTime"); } if (!string.IsNullOrWhiteSpace(model.LogEndTime)) { sqlParameters.Add(new SqlParameter("@LogEndTime", model.LogEndTime)); sqlWhere.Append(@" AND h.LogDate < @LogEndTime"); } if (!string.IsNullOrWhiteSpace(model.LogLevel)) { sqlParameters.Add(new SqlParameter("@LogLevel", model.LogLevel)); sqlWhere.Append(@" AND h.LogLevel = @LogLevel"); } if (!string.IsNullOrWhiteSpace(model.LogModule)) { sqlParameters.Add(new SqlParameter("@LogModule", model.LogModule)); sqlWhere.Append(@" AND h.LogModule = @LogModule"); } sql.AppendFormat(@" WITH t AS ( SELECT ROW_NUMBER() OVER ( ORDER BY id DESC ) AS IndexNum , [Id] , CONVERT(VARCHAR, [LogDate], 21) AS [LogDate] , [UserName] , SUBSTRING([Description], 0, 150) AS [Description] , SUBSTRING([LogMsg], 0, 200) AS [LogMsg] , [LogLevel] , [LogModule] FROM [LogSystem].[dbo].[xxxLog] h WHERE 1 = 1 {0} ) SELECT * FROM t WHERE IndexNum > @startIndex AND indexnum < @endIndex", sqlWhere); sqlParameters.Add(new SqlParameter("@startIndex", model.Start)); sqlParameters.Add(new SqlParameter("@endIndex", model.Start + model.Length)); DataTable dt = _sqlHelper.ExecuteDataTable(sql.ToString(), sqlParameters.ToArray()); return DataTableTools<LogModel>.DataTableToList(dt); } public int GetxxxLogTotalCount(SM_LogModel model) { StringBuilder sql = new StringBuilder(); List<SqlParameter> sqlParameters = new List<SqlParameter>(); sql.Append(@" SELECT COUNT(*) FROM [HdPubLog] h where 1=1 "); if (!string.IsNullOrWhiteSpace(model.LogStartTime)) { sqlParameters.Add(new SqlParameter("@LogStartTime", model.LogStartTime)); sql.Append(@" AND h.LogDate > @LogStartTime"); } if (!string.IsNullOrWhiteSpace(model.LogEndTime)) { sqlParameters.Add(new SqlParameter("@LogEndTime", model.LogEndTime)); sql.Append(@" AND h.LogDate < @LogEndTime"); } if (!string.IsNullOrWhiteSpace(model.LogLevel)) { sqlParameters.Add(new SqlParameter("@LogLevel", model.LogLevel)); sql.Append(@" AND h.LogLevel = @LogLevel"); } if (!string.IsNullOrWhiteSpace(model.LogModule)) { sqlParameters.Add(new SqlParameter("@LogModule", model.LogModule)); sql.Append(@" AND h.LogModule = @LogModule"); } return _sqlHelper.ExecuteScalar<int>(sql.ToString(), sqlParameters.ToArray()); } [HttpPost] public LogModel GetxxxxSignelLog(int id) { string sql = @" SELECT [Id] , CONVERT(VARCHAR(30), [LogDate], 21) AS [LogDate] , [UserName] , [Description] , [LogMsg] , [LogLevel] , [LogModule] , [Id] IndexNum FROM [LogSystem].[dbo].[xxxxLog] h WHERE h.id = @Id"; var row = _sqlHelper.ExecuteDataRow(sql, new SqlParameter("@Id", id)); return DataTableTools<LogModel>.DataRowToModel(row); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdppgw.html