MVC增加動作記錄

來源:互聯網
上載者:User

標籤:

在後台管理中,有一些操作是需要增加動作記錄的,尤其是對一些比較敏感的金額類的操作,比如商城類的修改商品金額、刪除商品、贈送金額等人工的操作。日誌中記錄著相關操作人的操作資訊,這樣,出了問題也容易排查。

那麼如何高效統一的處理增加這些日誌呢?下面,分享一下我的思路及做法。

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  
 

 

MVC增加動作記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.