0.引言
上一篇博文主要介绍了ABP模块及插件的相关知识,本章节主要开发一个插件示例来学习如何创建一个插件,并在应用程序中使用。这个命名为FirstABPPlugin的插件主要在指定的时间段内删除审计日志。
1.创建插件
(1).新建项目,选择【类库(.NET Core)】
(2).添加引用Abp、Abp.ZeroCore
(3).创建FirstABPPluginModule类,继承AbpModule类和声明依赖于AbpZeroCoreModule
[DependsOn(typeof(AbpZeroCoreModule))] public class FirstABPPluginModule:AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); } public override void PostInitialize() { var workManager = IocManager.Resolve<IBackgroundWorkerManager>(); workManager.Add(IocManager.Resolve(DeleteOldAuditLogsWorker()); } }
(4).添加DeleteOldAuditLogsWorker类
public class DeleteOldAuditLogsWorker : PeriodicBackgroundWorkerBase, ISingletonDependency { private readonly IRepository<AuditLog, long> _auditLogRepository; public DeleteOldAuditLogsWorker(AbpTimer timer,IRepository<AuditLog,long> auditLogRepository) : base(timer) { _auditLogRepository = auditLogRepository; Timer.Period = 5000; } [UnitOfWork] protected override void DoWork() { Logger.Info("---------------- DeleteOldAuditLogsWorker 正在工作 ----------------"); using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant)) { var fiveMinutesAgo = Clock.Now.Subtract(TimeSpan.FromMinutes(5)); _auditLogRepository.Delete(log => log.ExecutionTime > fiveMinutesAgo); CurrentUnitOfWork.SaveChanges(); } } }
(5).最终结构如下
(6).生成项目,在bin/Debug/netcoreapp2.1目录下生成FirstABPPlugin.dll
2.添加插件到应用程序
(1).启动ABP项目模版生成的程序,把刚生成的FirstABPPlugin.dll拷贝到wwwroot/Plugins目录下
(2).在Mvc项目的Startup.cs类中,添加如下代码:
public class Startup { private readonly IConfigurationRoot _appConfiguration; public Startup(IHostingEnvironment env) { _appConfiguration = env.GetAppConfiguration(); } public IServiceProvider ConfigureServices(IServiceCollection services) { ... // Configure Abp and Dependency Injection return services.AddAbp<AbpProjectNameWebMvcModule>( // Configure Log4Net logging options => options.IocManager.IocContainer.AddFacility<LoggingFacility>( f => f.UseAbpLog4Net().WithConfig( "log4net.config" ) ); options.PlugInSources.AddFolder(Path.Combine(_hostingEnvironment.WebRootPath, "Plugins" ), SearchOption.AllDirectories); ); } ... } |
(3)运行程序,查看Logs.txt日志记录
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦