MVC增加操作日志
在后臺管理中,有一些操作是需要增加操作日志的,尤其是對一些比較敏感的金額類的操作,比如商城類的修改商品金額、刪除商品、贈送金額等人工的操作。日志中記錄著相關(guān)操作人的操作信息,這樣,出了問題也容易排查。
那么如何高效統(tǒng)一的處理增加這些日志呢?下面,分享一下我的思路及做法。
1、建日志相關(guān)表。需要建兩個表,一是日志類型表(ActivityLogType),二是日志表(ActivityLog), 相關(guān)的表結(jié)構(gòu)如下:
日志類型表:Id,SystemKeyword,Name,Enable (1 自動投標(biāo)設(shè)置 自動投標(biāo)設(shè)置 0)
日志表:Id,ActivityLogTypeId,CustomerId,Comment,CreateTime
2、自定義一個屬性類,繼承ActionFilterAttribute
/// <summary>/// 業(yè)務(wù)日志/// </summary>public class BizActivityLogAttribute : ActionFilterAttribute{/// <summary>/// 參數(shù)名稱列表,可以用, | 分隔/// </summary>private readonly string _parameterNameList;//類型名稱private string _activityLogTypeName = "";/// <summary>/// 活動日志/// </summary>/// <param name="activityLogTypeName">類別名稱</param>/// <param name="parm">參數(shù)名稱列表,可以用, | 分隔</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);}}//日志內(nèi)容StringBuilder logContent = new StringBuilder();foreach (KeyValuePair<string, string> kvp in parmsObj){logContent.AppendFormat("{0}:{1} ",kvp.Key,kvp.Value);}//******************************************************************************//這里是插入數(shù)據(jù)表操作//步驟://1、根據(jù)日志類型表的SystemKeyword得到日志類型Id//2、往日志表里插入數(shù)據(jù),logContent.ToString()是內(nèi)容,內(nèi)容可以自己拼接字符串,比如:string.Format("刪除記錄,刪除操作者{0}","xxxx");var _customerActivityService = EngineContext.Current.Resolve<ICustomerActivityService>();_customerActivityService.InsertActivity(_activityLogTypeName, logContent.ToString(), workContext.CurrentCustomer, workContext.CurrentCustomer.Id);//******************************************************************************}}}3、在要寫日志的ActionResult里增加屬性標(biāo)識,很簡單,如:
參數(shù)寫法:
[BizActivityLog("新增激活碼", "activateCodeType,filePath")]public ActionResult Add(int? activateCodeType, string filePath){ }模型寫法:
[BizActivityLog("刪除激活碼", "RegNumber,CouponCode,ActivateCodeType,StartCreateDate,EndCreateDate,StartPresentedDate,EndPresentedDate")]public ActionResult Del(ActivateCodeSearchModel searchModel){ }?
BizActivityLog的第一個參數(shù)是SystemKeyword。
?
那么,最終將會往數(shù)據(jù)庫里增加類型下面的一條記錄:
16599?,804,274075?CustomerId:276638 Phone:18686556492 Amount:1000000 RealName:張三 BankName:中國人民銀行?2015-01-21 14:52:02.290??
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/jys509/p/4239670.html
總結(jié)
- 上一篇: ADO.NET学习笔记-非链接类
- 下一篇: s3c2440移植MQTT