namespace Admin.NET.Core; /// /// 清理日志作业任务 /// [JobDetail("job_log", Description = "清理操作日志", GroupName = "default", Concurrent = false)] [Daily(TriggerId = "trigger_log", Description = "清理操作日志")] public class LogJob : IJob { private readonly IServiceProvider _serviceProvider; public LogJob(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken) { using var serviceScope = _serviceProvider.CreateScope(); var logVisRep = serviceScope.ServiceProvider.GetService>(); var logOpRep = serviceScope.ServiceProvider.GetService>(); var logDiffRep = serviceScope.ServiceProvider.GetService>(); var daysAgo = 30; // 删除30天以前 await logVisRep.AsDeleteable().Where(u => (DateTime)u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除访问日志 await logOpRep.AsDeleteable().Where(u => (DateTime)u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除操作日志 await logDiffRep.AsDeleteable().Where(u => (DateTime)u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除差异日志 } }