Open Closed

When Does ABP’s Built-In Audit Log Cleanup Job Run and Can I Customize Its Execution Time? #9401


User avatar
0
Pallabi created

Hi team,

I’ve enabled ABP’s built-in audit log cleanup job via the Settings UI (enabled AuditLogging.Cleanup.IsEnabled and set a retention period).

  1. What time of day does this cleanup job run by default?
  2. Is there a supported way to customize the execution time?

Thanks!


1 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    I found its registration like below:

    await context.ServiceProvider
                .GetRequiredService<IBackgroundWorkerManager>()
                .AddAsync(context.ServiceProvider.GetRequiredService<ExpiredAuditLogDeleterWorker>());
    

    It works as background worker.

    It resolves ExpiredAuditLogDeleterOptions in the main ExpiredAuditLogDeleterWorker service. And that option class is like below:

    public class ExpiredAuditLogDeleterOptions
    {
        /// <summary>
        /// Default: Everyday once.
        /// </summary>
        public int Period { get; set; } = (int)TimeSpan.FromDays(1).TotalMilliseconds;
    }
    

    Unfortunately, there is no a CRON-like condiguration that identifies exact time to work right now. Here a suggestion about how you can do it manually:

    • Add Cronos libraryo to parse a CRON expressions:
    dotnet add package Cronos
    
    • Then create your own ExpiredAuditLogDeleterWorker service to replace the original one.
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(ExpiredAuditLogDeleterWorker))]
    public class MyCustomExpiredAuditLogDeleterWorker : ExpiredAuditLogDeleterWorker
    {
        // Your expression here:
        public const string Expression = "0 23 * * *";
        public MyCustomExpiredAuditLogDeleterWorker(
            AbpAsyncTimer timer,
            IServiceScopeFactory serviceScopeFactory,
            IOptions<ExpiredAuditLogDeleterOptions> options) : base(timer, serviceScopeFactory, options)
        {
            // Cron expressions resolution is 1 minute, so we need to set the period to 1 minute
            // Each minute, the worker will check if there are any expired audit logs and delete them
            timer.Period = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;
        }
    
        protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
        {
            var cronExpression = CronExpression.Parse(Expression);
            var now = DateTimeOffset.UtcNow;
            var nextOccurrence = cronExpression.GetNextOccurrence(now.AddSeconds(-1), TimeZoneInfo.Utc);
    
            // If the next occurrence is within this minute, run the job
            if (nextOccurrence.HasValue && 
                nextOccurrence.Value > now.AddSeconds(-60) && 
                nextOccurrence.Value <= now)
            {
                await base.DoWorkAsync(workerContext);
            }
            // else: do nothing, wait for the next tick
        }
    }
    

    It seems it's better to use background jobs instead background worker, but it's what it's right now. You can use this workaround and I'll inform the ABP team about it. They may want to make an enhancement

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on July 11, 2025, 11:35