Asp.net mvc:使用ActionFilterAttribute记录操作日志

1.ActionFilterAttribute的基本介绍: 
引用命名空间:using system.web.mvc,这很重要,在system.web.Http.Filter下也有ActionFilterAttribute类,但有着很大的区别。 
ActionFilterAttribute是Action过滤类,可在action执行之前,action执行之后,和提交操作结果之前和执行操作结果之后执行。 
2.构造操作日志的类

 public class OperateLog
    {
        /// <summary>
        /// 主键Id
        /// </summary>
        [DisplayName("主键Id")]
        public Guid Id { get; set; }
        /// <summary>
        /// 操作员Id
        /// </summary>
        [DisplayName("操作员Id")]
        public Guid OperatorId { get; set; }
        /// <summary>
        /// 操作员
        /// </summary>
        [DisplayName("操作员")]
        public Operator Operator { get; set; }
        /// <summary>
        /// 操作时间
        /// </summary>
        [DisplayName("操作时间")]
        public DateTime OperateTime { get; set; }
        /// <summary>
        /// 具体操作
        /// </summary>
        [DisplayName("具体操作")]
        public string Operate { get; set; }
        /// <summary>
        /// 重要级别
        /// </summary>
        [DisplayName("重要级别")]
        public int Level { get; set; }
        /// <summary>
        /// 重要级别
        /// </summary>
        [NotMapped]
        [DisplayName("重要级别")]
        public ImportantLevel LevelEnum
        {
            get { return (ImportantLevel)Level; }
            set { Level = (int)value; }
        }
        /// <summary>
        /// 描述
        /// </summary>
        [DisplayName("描述")]
        public string Description { get; set; }
        /// <summary>
        /// 重要级别
        /// </summary>
        public enum ImportantLevel { 一般操作 = 0, 危险操作 = 1 }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

3.构造对应的ActionFilterAttribute类,实现方法OnActionExecuted()可在Action执行之后记录操作日志。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class OperationLogAttribute: ActionFilterAttribute
    {
        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="operate">具体操作</param>
        public OperationLogAttribute(string operate,ImportantLevel level=ImportantLevel.一般操作 ,string description=null)
        {
           this.Operate = operate;
            Level = level;
            Description = description;
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (Common.Common.CurrentUser == null) return;

            AdministrativeSupervisionContext db = new AdministrativeSupervisionContext();
            OperateLog operateLog = new OperateLog();
            operateLog.Id = Guid.NewGuid();
            operateLog.OperateTime = DateTime.Now;
            operateLog.Operate = Operate;
            operateLog.Description = Description;
            operateLog.LevelEnum = Level;
            operateLog.OperatorId = Common.Common.CurrentUser.Id;
            db.OperateLogs.Add(operateLog);
            db.SaveChanges();
            base.OnActionExecuted(filterContext);
        }



        /// <summary>
        /// 操作员Id
        /// </summary>
        public Guid OperatorId { get; set; }
        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime OperateTime { get; set;  }
        /// <summary>
        /// 具体操作
        /// </summary>
        public string Operate { get; set; }
        /// <summary>
        /// 危险等级
        /// </summary>
        public ImportantLevel Level { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

3.应用构造的ActionFilterAttribute类,在需要记录操作日志的Action之前加入即可。

[Attributes.OperationLog("删除日志",OperateLog.ImportantLevel.危险操作)]
        public virtual async Task<ActionResult> DeleteConfirmed(Guid id)
        {
            try{
            OperateLog operateLog = await db.OperateLogs.FindAsync(id);
            db.OperateLogs.Remove(operateLog);
            await db.SaveChangesAsync();
                //return RedirectToAction("Index");
                return Json(new JsonResultModel { Status = (int)JsonResultStatus.Success, Message = "删除成功" });
            }catch{
                return Json(new JsonResultModel { Status = (int)JsonResultStatus.Error, Message = "删除时发生错误" });
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值