Keep history of jobs executed for more than 1 day in Hangfire

前端 未结 1 1702
傲寒
傲寒 2021-01-12 21:40

I\'ve just started using Hangfire, and I am loving it.

I understand that Hangfire maintains the history for succeeded jobs for 1 day, and clear it thereafter.

<
相关标签:
1条回答
  • 2021-01-12 22:21

    To do this, you need to create a job filter and register it through hangfire global configurations, as discussed here - https://discuss.hangfire.io/t/how-to-configure-the-retention-time-of-job/34

    Create job filter -

    using Hangfire.Common;
    using Hangfire.States;
    using Hangfire.Storage;
    using System;
    
    namespace HangfireDemo
    {
        public class ProlongExpirationTimeAttribute : JobFilterAttribute, IApplyStateFilter
        {
            public void OnStateApplied(ApplyStateContext filterContext, IWriteOnlyTransaction transaction)
            {
                filterContext.JobExpirationTimeout = TimeSpan.FromDays(7);
            }
    
            public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
            {
                context.JobExpirationTimeout = TimeSpan.FromDays(7);
            }
        }
    }
    

    ...and register in global job filters -

    GlobalJobFilters.Filters.Add(new ProlongExpirationTimeAttribute());
    
    0 讨论(0)
提交回复
热议问题