MVC拦截器记录操作用户日志

本文介绍了一个自定义属性类,用于ASP.NET MVC中记录用户的操作行为,包括操作的时间、方法名称、控制器名称等,并提供了如何使用该属性来追踪特定操作的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要是用于记录用户操作动态,

 1 public class OperationAttribute:ActionFilterAttribute
 2 {
 3 /// <summary>
 4 /// 方法名称
 5 /// </summary>
 6 public string ActionName { get; set; }
 7 /// <summary>
 8 /// 控制器名称
 9 /// </summary>
10 public string ControllerName { get; set; }
11 /// <summary>
12 /// 方法参数
13 /// </summary>
14 public string ActionParameters { get; set; }
15 /// <summary>
16 /// 访问时间
17 /// </summary>
18 public DateTime AccessDate { get; set; }
19 /// <summary>
20 /// 登录用户
21 /// </summary>
22 public string LoginName { get; set; }
23 /// <summary>
24 /// 操作备注
25 /// </summary>
26 public string Operationremark { get; set; }
27 /// <summary>
28 /// 是否记录入库
29 /// </summary>
30 public bool IsLog { get; set; }
31 /// <summary>
32 /// 操作模块描述
33 /// </summary>
34 public string ModuleName { get; set; }
35 /// <summary>
36 /// 操作动作
37 /// </summary>
38 public string Option { get; set; }
39 
40 /// <summary>
41 /// 操作人id
42 /// </summary>
43 public int adminid { get; set; }
44 /// <summary>
45 /// 操作人名
46 /// </summary>
47 public string adminName { get; set; }
48 
49 public OperationAttribute()
50 {
51 this.AccessDate = DateTime.Now;
52 this.IsLog = true;
53 }
54 
55 /// <summary>
56 /// 
57 /// </summary>
58 /// <param name="moduleName">操作模块描述</param>
59 /// <param name="option">操作动作描述</param>
60 /// <param name="remark">其他备注</param>
61 public OperationAttribute(string moduleName, string option,string remark="")
62 {
63 this.AccessDate = DateTime.Now;
64 this.IsLog = true;
65 this.ModuleName = moduleName;
66 this.Option = option;
67 this.Operationremark = remark;
68 }
69 public override void OnActionExecuting(ActionExecutingContext filterContext)
70 {
71 if (this.IsLog)
72 {
73 //方法名称
74 this.ActionName = filterContext.ActionDescriptor.ActionName;
75 //控制器
76 this.ControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
77 ///页面传递参数
78 IDictionary<string, object> dic = filterContext.ActionParameters;
79 var parameters = new System.Text.StringBuilder();
80 foreach (var item in dic)
81 {
82 parameters.Append(item.Key + "=" + item.Value + "|^|");
83 }
84 this.ActionParameters = parameters.ToString();
85 var userInfo = GetUserResultModel();
86 
87 //this.adminName = userInfo.userName;
88 
89 //this.adminid = userInfo.userid;
90 
91 //操作数据库记录
92  
93 
94 }
95 }

 

 

 

使用方法:

直接在action头上 加多特性[Operation("用户管理", "修改密码")] 就可以获取到用户操作的动作。

转载于:https://my.oschina.net/martin321/blog/1525320

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值