fix 删除日志try

This commit is contained in:
cheche
2024-03-19 18:36:14 +08:00
parent 1c7a054ed4
commit 9a7091b4e0

View File

@@ -7,25 +7,35 @@ namespace Dlhis.ScheduledTask.CommonJobs;
public class DelApiLogTaskJob public class DelApiLogTaskJob
{ {
private readonly IDbContext _dbContext; private readonly IDbContext _dbContext;
private readonly Logger<DelApiLogTaskJob> _logger;
public DelApiLogTaskJob(IDbContext dbContext) public DelApiLogTaskJob(IDbContext dbContext, Logger<DelApiLogTaskJob> logger)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_logger = logger;
} }
public async Task RunAsync() public async Task RunAsync()
{ {
DateTime now = DateTime.Now; try
DateTime todayStart = now.Date; {
DateTime threeDaysAgoStart = todayStart.AddDays(-3); DateTime now = DateTime.Now;
DateTime todayStart = now.Date;
DateTime threeDaysAgoStart = todayStart.AddDays(-3);
var logs = await _dbContext.ChangeDb("log") var logs = await _dbContext.ChangeDb("log")
.Queryable<ApiLogEntity>() .Queryable<ApiLogEntity>()
.Where(x => x._ts<= threeDaysAgoStart) .Where(x => x._ts <= threeDaysAgoStart)
.OrderBy(x =>x._ts,OrderByType.Asc) .OrderBy(x => x._ts, OrderByType.Asc)
.ToListAsync(); .ToListAsync();
await _dbContext.ChangeDb("log").DeleteReallyAsync(logs);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除日志异常:{message}", ex.Message);
}
await _dbContext.ChangeDb("log").DeleteReallyAsync(logs);
} }
} }