对于它的价值,我最终使用服务器/客户端过滤器来完成这个任务,并且GlobalJobFilters
注册如下所示。我遇到的一个恼人的问题是AutomaticRetryAttribute
默认添加到GlobalJobFilters
集合,以及该类将记录失败作业的错误 https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/AutomaticRetryAttribute.cs#L169不知道在我们自定义中创建的 Serilog LogContextJobLoggerAttribute
。就我个人而言,我知道我只允许手动重试,因此我只是删除了该属性并在IServerFilter.OnPerformed
方法。检查我的帖子的末尾,看看如何删除它(如果这对您有用)。
如果您要允许自动重试,那么您将需要:1)创建一个自定义属性来装饰AutomaticRetryAttribute
并使其了解自定义 LogContext,2) 再次删除默认值AutomaticRetryAttribute
来自GlobalJobFilters
集合,3) 将装饰器属性添加到集合中。
public class JobLoggerAttribute : JobFilterAttribute, IClientFilter, IServerFilter
{
private ILogger _log;
public void OnCreating(CreatingContext filterContext)
{
_log = GetLogger();
_log.Information("Job is being created for {JobType} with arguments {JobArguments}", filterContext.Job.Type.Name, filterContext.Job.Args);
}
public void OnCreated(CreatedContext filterContext)
{
_log.Information("Job {JobId} has been created.", filterContext.BackgroundJob.Id);
}
public void OnPerforming(PerformingContext filterContext)
{
if (_log == null)
_log = GetLogger();
_log.Information("Job {JobId} is performing.", filterContext.BackgroundJob.Id);
}
public void OnPerformed(PerformedContext filterContext)
{
_log.Information("Job {JobId} has performed.", filterContext.BackgroundJob.Id);
if (filterContext.Exception != null)
{
_log.Error(
filterContext.Exception,
"Job {JobId} failed due to an exception.",
filterContext.BackgroundJob.Id);
}
_log = null;
}
private ILogger GetLogger()
{
return Log.ForContext(GetType()).ForContext("HangfireRequestId", Guid.NewGuid());
}
}
还有登记...
GlobalJobFilters.Filters.Add(new JobLoggerAttribute());
删除AutomaticRetryAttribute
...
var automaticRetryFilter = GlobalJobFilters.Filters.Where(x => x.Instance is AutomaticRetryAttribute).Single();
GlobalJobFilters.Filters.Remove(automaticRetryFilter.Instance);