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  
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM