在Hangfire中将执行的工作记录保存1天以上 [英] Keep history of jobs executed for more than 1 day in Hangfire

查看:540
本文介绍了在Hangfire中将执行的工作记录保存1天以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Hangfire,现在我很喜欢它.

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

我了解Hangfire将成功工作的历史记录保留了1天,之后将其清除.

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

是否可以自定义此默认行为并将历史记录保留7天?

Is there is a way where I can customize this default behavior and persist the history for any duration say 7 days?

推荐答案

为此,您需要创建一个作业过滤器并通过hangfire全局配置对其进行注册,如下所述- https://discuss.hangfire.io /t/如何配置工作的保留时间/34

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

创建工作过滤器-

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

这篇关于在Hangfire中将执行的工作记录保存1天以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆