Nop-如何增加Nop的后台操作日志<十>

NopCommerce提供的一项功能是操作日志记录,例如XX用户在后台管理进行了XX操作:我们先看一下和ActivityLog相关的界面以及表。

当我需要增加一个自己的操作记录的时候,我们需要哪些操作?接下来一一道来:

  /****** Script forSelectTopNRows command from SSMS  ******/

SELECT TOP 1000 [Id]

      ,[SystemKeyword]

      ,[Name]

      ,[Enabled]

  FROM [nop].[dbo].[ActivityLogType]

文件:nopcommerce\Libraries\Nop.Services\Installation\InstallationService.cs

 

protected virtual void InstallActivityLogTypes()

        {

            var activityLogTypes = newList<ActivityLogType>()

                                      {

                                          new ActivityLogType

                                              {

                                                 SystemKeyword = "AddNewCategory",

                                                 Enabled = true,

                                                 Name = "Add a new category"

                                             },

增加:在安装过程:

new ActivityLogType

                                             {

                                                 SystemKeyword = "AddSnatchItem",

                                                 Enabled = true,

                                                 Name = "Add a new snatch"                                              },


在数据库层次:  insert into [nop].[dbo].[ActivityLogType]values('AddSnatchItem','Add a new snatch',1)

由于我们并不需要修改数据表结构,在Core和Data两层可以忽略,在Service层Nop\Library\service中CustomerActivitySerivce的对应实现:

  

public virtual ActivityLogInsertActivity(string systemKeyword, string comment)

        {

            returnInsertActivity(systemKeyword, comment, new object[0]);

        }

 

        /// <summary>

        /// Inserts an activity log item

        /// </summary>

        /// <paramname="systemKeyword">The systemkeyword</param>

        /// <paramname="comment">The activitycomment</param>

        /// <paramname="commentParams">The activitycomment parameters for string.Format() function.</param>

        /// <returns>Activity log item</returns>

        public virtual ActivityLogInsertActivity(string systemKeyword, 

            stringcomment, params object[]commentParams)

        {

            if(_workContext.CurrentCustomer == null)

                returnnull;

 

            varactivityTypes = GetAllActivityTypes();

            varactivityType = activityTypes.ToList().Find(at => at.SystemKeyword ==systemKeyword);

            if(activityType == null || !activityType.Enabled)

                returnnull; 

            comment = CommonHelper.EnsureNotNull(comment);

            comment = string.Format(comment,commentParams);

            comment = CommonHelper.EnsureMaximumLength(comment,4000);    

 

            varactivity = new ActivityLog();

            activity.ActivityLogType =activityType;

            activity.Customer =_workContext.CurrentCustomer;

            activity.Comment = comment;

            activity.CreatedOnUtc = DateTime.UtcNow;

 

           _activityLogRepository.Insert(activity);

 

            returnactivity;

        }

在Controller Action中使用ActivityLog功能:

基本不需要修改,首先注册SystemKeyWord,在表activityLogTypes中(通过安装方式的话,在InstallationService中)在Controller的构造函数中增加IcustomerActivityservice,在需要使用的地方调用InsertActivity即可,SystemKeyWord需要Match。

  

//activity log

               _customerActivityService.InsertActivity("AddNewCategory",_localizationService.GetResource("ActivityLog.AddNewCategory"),category.Name);


 

函数定义:  ActivityLogInsertActivity(string systemKeyword, string comment, paramsobject[] commentParams);

 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值