欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

MVC增加操作日志

发布时间:2025/6/17 c/c++ 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 MVC增加操作日志 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在后台管理中,有一些操作是需要增加操作日志的,尤其是对一些比较敏感的金额类的操作,比如商城类的修改商品金额、删除商品、赠送金额等人工的操作。日志中记录着相关操作人的操作信息,这样,出了问题也容易排查。

那么如何高效统一的处理增加这些日志呢?下面,分享一下我的思路及做法。

1、建日志相关表。需要建两个表,一是日志类型表(ActivityLogType),二是日志表(ActivityLog), 相关的表结构如下:

日志类型表:Id,SystemKeyword,Name,Enable (1 自动投标设置 自动投标设置 0)

日志表:Id,ActivityLogTypeId,CustomerId,Comment,CreateTime

2、自定义一个属性类,继承ActionFilterAttribute

/// <summary>/// 业务日志/// </summary>public class BizActivityLogAttribute : ActionFilterAttribute{/// <summary>/// 参数名称列表,可以用, | 分隔/// </summary>private readonly string _parameterNameList;//类型名称private string _activityLogTypeName = "";/// <summary>/// 活动日志/// </summary>/// <param name="activityLogTypeName">类别名称</param>/// <param name="parm">参数名称列表,可以用, | 分隔</param>public BizActivityLogAttribute(string activityLogTypeName, string parm){_activityLogTypeName = activityLogTypeName;_parameterNameList = parm;}public override void OnActionExecuted(ActionExecutedContext filterContext){var workContext = EngineContext.Current.Resolve<IWorkContext>();if (workContext != null && workContext.CurrentCustomer != null){Dictionary<string, string> parmsObj = new Dictionary<string, string>();foreach (var item in _parameterNameList.Split(',', '|')){var valueProviderResult = filterContext.Controller.ValueProvider.GetValue(item);if (valueProviderResult != null && !parmsObj.ContainsKey(item)){parmsObj.Add(item, valueProviderResult.AttemptedValue);}}//日志内容StringBuilder logContent = new StringBuilder();foreach (KeyValuePair<string, string> kvp in parmsObj){logContent.AppendFormat("{0}:{1} ",kvp.Key,kvp.Value);}//******************************************************************************//这里是插入数据表操作//步骤://1、根据日志类型表的SystemKeyword得到日志类型Id//2、往日志表里插入数据,logContent.ToString()是内容,内容可以自己拼接字符串,比如:string.Format("删除记录,删除操作者{0}","xxxx");var _customerActivityService = EngineContext.Current.Resolve<ICustomerActivityService>();_customerActivityService.InsertActivity(_activityLogTypeName, logContent.ToString(), workContext.CurrentCustomer, workContext.CurrentCustomer.Id);//******************************************************************************}}}

3、在要写日志的ActionResult里增加属性标识,很简单,如:

参数写法:

[BizActivityLog("新增激活码", "activateCodeType,filePath")]public ActionResult Add(int? activateCodeType, string filePath){ }

模型写法:

[BizActivityLog("删除激活码", "RegNumber,CouponCode,ActivateCodeType,StartCreateDate,EndCreateDate,StartPresentedDate,EndPresentedDate")]public ActionResult Del(ActivateCodeSearchModel searchModel){ }

 

BizActivityLog的第一个参数是SystemKeyword。

 

那么,最终将会往数据库里增加类型下面的一条记录:

16599 804,274075 CustomerId:276638 Phone:18686556492 Amount:1000000 RealName:张三 BankName:中国人民银行 2015-01-21 14:52:02.290  
 

 

转载于:https://www.cnblogs.com/jys509/p/4239670.html

总结

以上是生活随笔为你收集整理的MVC增加操作日志的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。