Keep a history of jobs completed over 1 day in Hangfire

I just started using Hangfire and I love it.

I understand that Hangfire maintains a 1-day success story and clears it after that.

Is there a way I can set this default behavior and keep the history for any duration, for example, 7 days?

+4
source share
1 answer

To do this, you need to create a task filter and register it through the global hangfire configurations, as described 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 task filters -

GlobalJobFilters.Filters.Add(new ProlongExpirationTimeAttribute());
+6
source

Source: https://habr.com/ru/post/1621601/


All Articles