软件开发框架操作日志错误日志的使用方法

日志在软件开发框架中是必然的组件,开发框架中提供了操作、错误和审计三种日志记录机制,那么在实际项目开发中该怎么去使用呢?

怎么用?

日志使用图例

如上图结果图,我们需要调用日志方法往数据库中插入日志操作数据:

	//添加操作日志
        OprLog(new OprLogIn() { Controller = "ResumeMain", Action = "Save", UserName = UserName, OperationType = "简历主从表", Data = "" }); 
	//添加错误日志
        ErrorLog(new ErroInrLog() { Url = "ResumeMain/Save", UserName = UserName, Message = ex.ToString().Substring(0,1000), 
        ErrorType = ErrorType.GeneralError.ToString(), Data = "错误" }); 

怎么用?照上面的依葫芦画瓢就可以,那么后面是个什么机制呢?

机制原理

这个其实也没有什么复杂的机制,就是建三个类型的数据模型,然后建数据库表,然后把三种类型的数据存入相应的表即可,三个数据模型关键数据参数,具体见软件开发框架源代码:

审计日志模型 AuditLogInViewModel.cs

    public partial class AuditLogInViewModel
    {
        public int Id { get; set; }
        [Display(Name = "来源Id")] 
        public int? FromId { get; set; } 
        
        [Display(Name = "菜单标题")] 
        public string Title { get; set; } 
        
        [Display(Name = "IP")] 
        public string IP { get; set; } 
        
        [Display(Name = "表名")] 
        public string TableName { get; set; } 
        
        [Display(Name = "更改列")] 
        public string Column { get; set; } 
        
        [Display(Name = "新值")] 
        public string NewVal { get; set; } 
        
        [Display(Name = "旧值")] 
        public string OldVal { get; set; } 
        
        [Display(Name = "添加时间")] 
        public DateTime? AddTime { get; set; } 
        
        [Display(Name = "操作人")] 
        public int? SystemUserId { get; set; } 
        
        [Display(Name = "备注")] 
        public string Remark { get; set; } 
        
        [Display(Name = "时间")] 
        public string AddDate { get; set; } 
        
        [Display(Name = "用户名")] 
        public string SystemUserName { get; set; } 
    }

操作日志数据模型OprLogInViewModel.cs

    public partial class OprLogInViewModel
    {
        public int Id { get; set; }
        [Display(Name = "控制器名称")] 
        public string Controller { get; set; } 
        
        [Display(Name = "参数或者返回数据")] 
        public string Data { get; set; } 
        
        [Display(Name = "操作人")] 
        public string UserName { get; set; } 
        
        [Display(Name = "操作类型")] 
        public string OperationType { get; set; } 
        
        [Display(Name = "请求的方法名称")] 
        public string Action { get; set; } 
        
        [Display(Name = "请求的连接")] 
        public string Url { get; set; } 
        
        [Display(Name = "操作时间")] 
        public DateTime? AddTime { get; set; } 
   }

错误日志数据模型ErroInrLogViewModel.cs

    public partial class ErroInrLogViewModel
    {
        public int Id { get; set; }
        [Display(Name = "Url")] 
        public string Url { get; set; } 
        
        [Display(Name = "错误源")] 
        public string Source { get; set; } 
        
        [Display(Name = "数据")] 
        public string Data { get; set; } 
        
        [Display(Name = "异常全信息")] 
        public string InnerException { get; set; } 
        
        [Display(Name = "异常信息")] 
        public string Message { get; set; } 
        
        [Display(Name = "堆栈信息")] 
        public string StackTrace { get; set; } 
        
        [Display(Name = "当前异常的方法")] 
        public string TargetSite { get; set; } 
        
        [Display(Name = "出错的类名")] 
        public string DeclaringTypeName { get; set; } 
        
        [Display(Name = "状态码")] 
        public string StatusCode { get; set; } 
        
        [Display(Name = "添加时间")] 
        public DateTime? AddTime { get; set; } 
        
        [Display(Name = "类型")] 
        public string Type { get; set; } 
        
        [Display(Name = "错误类型")] 
        public string ErrorType { get; set; } 
        
        [Display(Name = "用户名称")] 
        public string UserName { get; set; } 
	}

模型建好后,那就是基本的增删改查了, OprLog和ErrorLog方法的实现在软件开发框架控制器基类BaseController里,代码如下:

        /// <summary>
        /// 错误日志记录
        /// </summary>
        /// <param name="erroInrLog"></param>
        public void ErrorLog(ErroInrLog erroInrLog)
        {
            if (erroInrLog == null)
            {
                return;
            }
            var _erroInrLogService = ResolveService<IErroInrLogService>();
            erroInrLog.CreateBy = this.CurrentUser.LoginAccount;
            erroInrLog.CreateAt = DateTime.Now;
            erroInrLog.AddTime = DateTime.Now;
            erroInrLog.UserName = UserName;
            Task.Run(() =>
            {
                _erroInrLogService.CreateErroInrLog(erroInrLog);
            });

        }

        /// <summary>
        /// 错误日志记录
        /// </summary>
        /// <param name="erroInrLogList"></param>
        public void ErrorLogs(List<ErroInrLog> erroInrLogList)
        {

            if (erroInrLogList == null || erroInrLogList.Count <= 0)
            {
                return;
            }
            foreach (var erroInrLog in erroInrLogList)
            {
                erroInrLog.CreateBy = this.CurrentUser.LoginAccount;
                erroInrLog.CreateAt = DateTime.Now;
                erroInrLog.AddTime = DateTime.Now;
                erroInrLog.UserName = UserName;

            }
            var _erroInrLogService = ResolveService<IErroInrLogService>();
            Task.Run(() =>
            {
                _erroInrLogService.CreateErroInrLogList(erroInrLogList);
            });

        }

        /// <summary>
        /// 操作日志记录
        /// </summary>
        /// <param name="oprLogIn"></param>
        public void OprLog(OprLogIn oprLogIn)
        {
            if (oprLogIn == null)
            {
                return;
            }
            var _oprloginService = ResolveService<IOprLogInService>();
            oprLogIn.CreateBy = this.CurrentUser.LoginAccount;
            oprLogIn.CreateAt = DateTime.Now;
            oprLogIn.AddTime = DateTime.Now;
            oprLogIn.UserName = UserName;
            Task.Run(() =>
            {
                _oprloginService.CreateOprLogIn(oprLogIn);
            });
        }

        /// <summary>
        /// 操作日志记录
        /// </summary>
        /// <param name="auditLogList"></param>
        public void OprLogs(List<OprLogIn> oprLogList)
        {
            if (oprLogList == null || oprLogList.Count <= 0)
            {
                return;
            }
            foreach (var oprLog in oprLogList)
            {
                oprLog.CreateBy = this.CurrentUser.LoginAccount;
                oprLog.CreateAt = DateTime.Now;
                oprLog.UserName = UserName;
                oprLog.AddTime = DateTime.Now;
            }
            var _oprloginService = ResolveService<IOprLogInService>();
            Task.Run(() =>
            {
                _oprloginService.CreateOprLogInList(oprLogList);
            });
        }

        /// <summary>
        /// 审计日志记录
        /// </summary>
        /// <param name="auditLog"></param>
        public void AuditLog(AuditLogIn auditLog)
        {
            if (auditLog == null)
            {
                return;
            }
            var _auditloginService = ResolveService<IAuditLogInService>();
            auditLog.CreateBy = this.CurrentUser.LoginAccount;
            auditLog.CreateAt = DateTime.Now;
            auditLog.AddDate = DateTime.Now.ToString();
            auditLog.AddTime = DateTime.Now;
            auditLog.SystemUserId = UserID;
            auditLog.SystemUserName = UserName;
            auditLog.IP = HttpHelper.GetWebClientIp();
            Task.Run(() =>
            {
                _auditloginService.CreateAuditLogIn(auditLog);
            });
        }

        /// <summary>
        /// 审计日志记录
        /// </summary>
        /// <param name="auditLogList"></param>
        public void AuditLogs(List<AuditLogIn> auditLogList)
        {

            var list = auditLogList.Where(p => p.OldVal != p.NewVal).ToList();
            if (list == null || list.Count <= 0)
            {
                return;
            }
            foreach (var auditLog in list)
            {
                auditLog.CreateBy = this.CurrentUser.LoginAccount;
                auditLog.CreateAt = DateTime.Now;
                auditLog.AddDate = DateTime.Now.ToString();
                auditLog.AddTime = DateTime.Now;
                auditLog.SystemUserId = UserID;
                auditLog.SystemUserName = UserName;
                auditLog.IP = HttpHelper.GetWebClientIp();
            }
            var _auditloginService = ResolveService<IAuditLogInService>();
            Task.Run(() =>
            {
                _auditloginService.CreateAuditLogInList(list);
            });
        }

操作方法 

软件开发框架日志操作方法

页签:日志 操作日志 错误日志 审计日志

标签: Web开发框架开发指南红宝书

网站&系统开发技术学习交流群:463167176

本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
转载请注明:文章转载自:软件开发框架 » 软件开发框架操作日志错误日志的使用方法
本文标题:软件开发框架操作日志错误日志的使用方法
本文地址:https://www.hocode.com/OrgTec/Back/0038.html

相关文章: 快速开发平台消息通知中心之短信发送

电话
电话 18718672256

扫一扫
二维码