.net 日志系统解析

一.   写在前面

日志系统对于任何项目都是必不可少的,无论对于测试阶段的debug,性能测试,执行时间,操作记录还是线上的问题排查,访问记录等,日志系统都扮演着重要的角色。本篇分享的目的是能帮助需要的人快速搭建自己的LogSystem.,仅供参考。 先上个图呗,自认为页面还算清爽吧:

我的LogSystem使用Log4net入库的方式,网上特别多的分享,但是能完整运行下来的真是很少,所以现在需要和以后用得上的小伙伴抓紧收藏咯。

.net 日志系统解析

.net 日志系统解析

二.  Log4Net自定义内容入库

Log4Net存日志的方式,给人的感觉实在是不实用,IT行业不都求一个自动化吗?废话不说了,先上Log4net入库系统的代码。

LogSystem数据库结构,我的建议是一个项目一个表。

.net 日志系统解析

在Log组件中,你需要这样几个类。下面分别给出代码:

.net 日志系统解析

LogContent.cs,这里定义了Log实体,在实体化实体的时候,通过给构造函数传参创建好这个对象。注释很详细了

using System; namespace LogComponent { public class LogContent { public LogContent(string logLevel, string logMsg, string logModule, string description, string userName) { LogLevel = logLevel; UserName = userName; Description = description; LogMsg = logMsg; LogModule = logModule; } /// <summary> /// 日志级别 /// </summary> public string LogLevel { get; set; } /// <summary> /// 日志消息 /// </summary> public string LogMsg { get; set; } /// <summary> /// 系统登陆用户 /// </summary> public string UserName { get; set; } /// <summary> /// 日志描述信息 /// </summary> public string Description { get; set; } /// <summary> /// 记录时间 /// </summary> public DateTime LogDate { get; set; } /// <summary> /// 模块名称 /// </summary> public string LogModule { get; set; } } }

LogHelper.cs,定义了日志级别,和写入方法

[assembly: log4net.Config.XmlConfigurator(Watch = true,ConfigFile = "log4net.config")] namespace LogComponent { public class LogHelper { static log4net.ILog log = log4net.LogManager.GetLogger("myLogger"); /// <summary> /// 异常日志 /// </summary> /// <param>日志信息</param> /// <param>代码模块</param> /// <param>其他描述</param> /// <param>用户名</param> public static void LogError(string logMsg, string logModule, string description = "", string userName = "") { log.Error(new LogContent("Error", SubLogString(logMsg), logModule, SubLogString(description), userName)); } public static void LogInfo(string logMsg, string logModule, string description = "", string userName = "") { log.Info(new LogContent("Info", SubLogString(logMsg), logModule, SubLogString(description), userName)); } public static void LogWarn(string logMsg, string logModule, string description = "", string userName = "") { log.Warn(new LogContent("Warn", SubLogString(logMsg), logModule, SubLogString(description), userName)); } public static void LogDebug(string logMsg, string logModule, string description = "", string userName = "") { log.Debug(new LogContent("Debug", SubLogString(logMsg), logModule, SubLogString(description), userName)); } private static string SubLogString(string str) { if (str.Length > 1500) { return str.Substring(0, 1500); } return str; } } }

MessagePartternConverter.cs

using log4net.Core; using log4net.Layout.Pattern; using System.IO; using System.Reflection; namespace LogComponent { class MessagePatternConverter : PatternLayoutConverter { protected override void Convert(TextWriter writer, LoggingEvent loggingEvent) { if (Option != null) { // Write the value for the specified key WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent)); } else { // Write all the key value pairs WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties()); } } /// <summary> /// 通过反射获取传入的日志对象的某个属性的值 /// </summary> /// <param></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; } } }

MyLayout.cs

using log4net.Layout; namespace LogComponent { class MyLayout : PatternLayout { public MyLayout() { this.AddConverter("property", typeof(MessagePatternConverter)); } } }

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

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