【问题标题】:Keep history of jobs executed for more than 1 day in Hangfire保留在 Hangfire 中执行超过 1 天的作业历史记录
【发布时间】:2015-12-24 10:00:11
【问题描述】:

我刚开始使用 Hangfire,我很喜欢它。

我了解 Hangfire 会将成功作业的历史记录保留 1 天,然后将其清除。

有没有一种方法可以自定义此默认行为并将历史记录保留 7 天?

【问题讨论】:

    标签: hangfire


    【解决方案1】:

    为此,您需要创建一个作业过滤器并通过 hangfire 全局配置对其进行注册,如此处所述 - https://discuss.hangfire.io/t/how-to-configure-the-retention-time-of-job/34

    创建工作过滤器 -

    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);
            }
        }
    }
    

    ...并在全局作业过滤器中注册 -

    GlobalJobFilters.Filters.Add(new ProlongExpirationTimeAttribute());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多