diff --git a/Tiobon.Core.IServices/ICommonServices.cs b/Tiobon.Core.IServices/ICommonServices.cs index 2f070fcf..cd3c71b3 100644 --- a/Tiobon.Core.IServices/ICommonServices.cs +++ b/Tiobon.Core.IServices/ICommonServices.cs @@ -37,4 +37,6 @@ public interface ICommonServices : IBaseServices> Task> getPostCommonAES(JObject param); Task> getPostCommon(JObject param); + + Task AutoClearLog(); } diff --git a/Tiobon.Core.Services/CommonServices.cs b/Tiobon.Core.Services/CommonServices.cs index a4bd511a..d07b6ba8 100644 --- a/Tiobon.Core.Services/CommonServices.cs +++ b/Tiobon.Core.Services/CommonServices.cs @@ -4252,4 +4252,33 @@ public partial class CommonServices : BaseServices>, ICommon return await QueryLangValue(key, App.User.GetLangId(), defaultValue); } #endregion + + #region 自动清理log + public async Task AutoClearLog() + { + var list = new List + { + "GlobalErrorLog", + "GlobalInformationLog", + "GlobalWarningLog" + }; + var dt = DateTime.Now.AddMonths(-3); + + for (int i = 0; i < list.Count; i++) + { + var tableName = $"{list[i]}_{dt.Year}"; + if (dt.Month < 10) + tableName += $"0{dt.Month}01"; + else + tableName += $"{dt.Month}01"; + + var sql = @$"IF EXISTS + (SELECT 1 + FROM sysobjects + WHERE id = object_id ('{tableName}') AND type = 'U') + DROP TABLE {tableName}"; + await Db.Ado.ExecuteCommandAsync(sql); + } + } + #endregion } diff --git a/Tiobon.Core.Tasks/QuartzNet/Jobs/Job_AutoClearLog_Quartz.cs b/Tiobon.Core.Tasks/QuartzNet/Jobs/Job_AutoClearLog_Quartz.cs new file mode 100644 index 00000000..ffe80dec --- /dev/null +++ b/Tiobon.Core.Tasks/QuartzNet/Jobs/Job_AutoClearLog_Quartz.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.Logging; +using Quartz; +using Tiobon.Core.IServices; + +/// +/// 这里要注意下,命名空间和程序集是一样的,不然反射不到(任务类要去JobSetup添加注入) +/// +namespace Tiobon.Core.Tasks; + +public class Job_AutoClearLog_Quartz : JobBase, IJob +{ + private readonly ILogger _logger; + private readonly ICommonServices _commonServices; + + public Job_AutoClearLog_Quartz(ILogger logger, ITasksQzServices tasksQzServices, ITasksLogServices tasksLogServices, ICommonServices commonServices) + : base(tasksQzServices, tasksLogServices) + { + _tasksQzServices = tasksQzServices; + _logger = logger; + _commonServices = commonServices; + } + public async Task Execute(IJobExecutionContext context) + { + // 可以直接获取 JobDetail 的值 + var jobKey = context.JobDetail.Key; + var jobId = jobKey.Name; + var executeLog = await ExecuteJob(context, async () => await Run(context, jobId.ObjToInt())); + + } + public async Task Run(IJobExecutionContext context, int jobid) + { + if (jobid > 0) + await _commonServices.AutoClearLog(); + } +}