Log4Net 添加自定义字段并保存到数据库
发布日期:2021-09-08 22:54:51 浏览次数:21 分类:技术文章

本文共 5225 字,大约阅读时间需要 17 分钟。

Log4Net是常用的功能强大的日志插件,该插件提供了几个默认字段

大家可能都用过Log4Net插件来记录日志,该插件默认提供了这几个字段@log_date, @thread, @log_level, @logger, @message, @exception, 但有时这几个字段不能满足我们记录日志的需求,需要扩展我们所需的其他字段,并记录到数据库。比如,我需要记录UserID,UnitCode,MenuID,OperateType等。下面按步骤介绍一下如何在Log4Net组件下,自定义字段并保存到数据库:

第一步:在数据库日志表中添加自定义字段

第二步:创建Custom实体ActionLoggerInfo

public class ActionLoggerInfo    {        public int UserID { get; set; }        public string UnitCode { get; set; }        public int MenuID { get; set; }        public int OperaterType { get; set; }        public string sMessage { get; set; }        public ActionLoggerInfo(int userId, string unitCode, int menuId, int operaterType,string smessage)        {            this.UserID = userId;            this.UnitCode = unitCode;            this.MenuID = menuId;            this.OperaterType = operaterType;            this.sMessage = smessage;        }    }

第三步:创建Custom转换器ActionConverter

 

/*命名空间引用*/using log4net;using log4net.Layout;using log4net.Layout.Pattern;using log4net.Core;/*代码*/public class ActionConverter : PatternLayoutConverter    {        protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)        {            var actionInfo = loggingEvent.MessageObject as ActionLoggerInfo;            if (actionInfo == null)            {                writer.Write("");            }            else            {                switch (this.Option.ToLower())                {                    case "userid":                        writer.Write(actionInfo.UserID);                        break;                    case "unitcode":                        writer.Write(actionInfo.UnitCode);                        break;                    case "menuid":                        writer.Write(actionInfo.MenuID);                        break;                    case "operatertype":                        writer.Write(actionInfo.OperaterType);                        break;                    case "smessage":                        writer.Write(actionInfo.sMessage);                        break;                    default:                        writer.Write("");                        break;                }            }        }    }

 

第四步:创建ActionLayoutPattern

/*命名空间引用*/using log4net;using log4net.Layout;using log4net.Layout.Pattern;using log4net.Core;/*代码*/public class ActionLayoutPattern : PatternLayout    {        public ActionLayoutPattern()        {            this.AddConverter("actionInfo", typeof(ActionConverter));        }    }

第五步:配置log4net.config

第六步:创建Log日志帮助类,LogUtil

 

public class LogUtil{    public LogUtil() { }        private static ActionLoggerInfo _message = null;        private static log4net.ILog _log;        public static log4net.ILog Log        {            get            {                if (_log == null)                {                     _log = LogManager.GetLogger("OperateLogger");                                 }                return _log;            }        }        public static void Debug()        {            if (Log.IsDebugEnabled)            {                Log.Debug(_message);            }        }        public static void Error()        {            if (Log.IsErrorEnabled)            {                Log.Error(_message);            }        }        public static void Fatal()        {            if (Log.IsFatalEnabled)            {                Log.Fatal(_message);            }        }        public static void Info()        {            if (Log.IsInfoEnabled)            {                Log.Info(_message);            }        }        public static void Warn()        {            if (Log.IsWarnEnabled)            {                Log.Warn(_message);            }        }        public static void SaveMessage(int userId, string unitCode, int menuId, int operaterType,string smessage,int level)        {            _message = new ActionLoggerInfo(userId, unitCode, menuId, operaterType, smessage);            switch (level)            {                case 1: Info(); break;                case 2: Warn(); break;                case 3: Error(); break;                case 4: Fatal(); break;                default: break;            }        }}

 

第七步:在具体页面上调用SaveMessage方法,写入日志

 

转载于:https://www.cnblogs.com/niuniu1985/p/7943463.html

转载地址:https://blog.csdn.net/weixin_34341229/article/details/93354844 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:第二周编程总结
下一篇:信息资源5

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月05日 08时07分08秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2019-04-27
Druid连接池实现自定义场景的多数据库的连接 2019-04-27
CentOs7命令行(静默)的方式安装oracle数据库 2019-04-27
基于VMware安装CentOs7的镜像 2019-04-27
PL/SQL数据库管理工具的使用 2019-04-27
史上最简单的spring-boot集成websocket的实现方式 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分库解决方案(二) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之关联查询解决方案(三) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之基于seata的分布式事务的解决方案(十五) 2019-04-27
Linux文件管理参考 2019-04-27
FTP文件管理项目(本地云)项目日报(一) 2019-04-27
FTP文件管理项目(本地云)项目日报(二) 2019-04-27
FTP文件管理项目(本地云)项目日报(三) 2019-04-27
FTP文件管理项目(本地云)项目日报(四) 2019-04-27
【C++】勉强能看的线程池详解 2019-04-27