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

 

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

分类: Nop Commerce   24人阅读  评论(0)  收藏  举报

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

 

[csharp]  view plain copy
  1. protected virtual void InstallActivityLogTypes()  
  2.   
  3.         {  
  4.   
  5.             var activityLogTypes = newList<ActivityLogType>()  
  6.   
  7.                                       {  
  8.   
  9.                                           new ActivityLogType  
  10.   
  11.                                               {  
  12.   
  13.                                                  SystemKeyword = "AddNewCategory",  
  14.   
  15.                                                  Enabled = true,  
  16.   
  17.                                                  Name = "Add a new category"  
  18.   
  19.                                              },  
  20.   
  21. 增加:在安装过程:  
  22.   
  23. new ActivityLogType  
  24.   
  25.                                              {  
  26.   
  27.                                                  SystemKeyword = "AddSnatchItem",  
  28.   
  29.                                                  Enabled = true,  
  30.   
  31.                                                  Name = "Add a new snatch"                                              },  


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

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

  

[csharp]  view plain copy
  1. public virtual ActivityLogInsertActivity(string systemKeyword, string comment)  
  2.   
  3.         {  
  4.   
  5.             returnInsertActivity(systemKeyword, comment, new object[0]);  
  6.   
  7.         }  
  8.   
  9.    
  10.   
  11.         /// <summary>  
  12.   
  13.         /// Inserts an activity log item  
  14.   
  15.         /// </summary>  
  16.   
  17.         /// <paramname="systemKeyword">The systemkeyword</param>  
  18.   
  19.         /// <paramname="comment">The activitycomment</param>  
  20.   
  21.         /// <paramname="commentParams">The activitycomment parameters for string.Format() function.</param>  
  22.   
  23.         /// <returns>Activity log item</returns>  
  24.   
  25.         public virtual ActivityLogInsertActivity(string systemKeyword,   
  26.   
  27.             stringcomment, params object[]commentParams)  
  28.   
  29.         {  
  30.   
  31.             if(_workContext.CurrentCustomer == null)  
  32.   
  33.                 returnnull;  
  34.   
  35.    
  36.   
  37.             varactivityTypes = GetAllActivityTypes();  
  38.   
  39.             varactivityType = activityTypes.ToList().Find(at => at.SystemKeyword ==systemKeyword);  
  40.   
  41.             if(activityType == null || !activityType.Enabled)  
  42.   
  43.                 returnnull;   
  44.   
  45.             comment = CommonHelper.EnsureNotNull(comment);  
  46.   
  47.             comment = string.Format(comment,commentParams);  
  48.   
  49.             comment = CommonHelper.EnsureMaximumLength(comment,4000);      
  50.   
  51.    
  52.   
  53.             varactivity = new ActivityLog();  
  54.   
  55.             activity.ActivityLogType =activityType;  
  56.   
  57.             activity.Customer =_workContext.CurrentCustomer;  
  58.   
  59.             activity.Comment = comment;  
  60.   
  61.             activity.CreatedOnUtc = DateTime.UtcNow;  
  62.   
  63.    
  64.   
  65.            _activityLogRepository.Insert(activity);  
  66.   
  67.    
  68.   
  69.             returnactivity;  
  70.   
  71.         }  

在Controller Action中使用ActivityLog功能:

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

  

[csharp]  view plain copy
  1. //activity log  
  2.   
  3.                _customerActivityService.InsertActivity("AddNewCategory",_localizationService.GetResource("ActivityLog.AddNewCategory"),category.Name);  


 

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

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值