Hi All,
I am planning to implement a logging feature on Hangfire Batch Job failure after all retries. The same can be achieved by creating a custom filter implementing IApplyStateFilter and getting NewState of AppliedStateContext. Iam getting the job id in this. But Iam not sure how to extract the batch ID for logging. Is there any means to do this.
Code Sample
batchJobClient.StartNew(x =>
{
var id1= x.Enqueue(() => Job1());
var id2= x.ContinueWith(id1, () => Job2(), JobContinuationOptions.OnlyOnSucceededState);
var id3= x.ContinueWith(id3, () => Job3(), JobContinuationOptions.OnlyOnSucceededState);
},“test”);
public class FailFilterAttribute : JobFilterAttribute, IApplyStateFilter
{
private static readonly ILog Logger = LogProvider.GetCurrentClassLogger();
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
var failedState = context.NewState as FailedState;
if (failedState != null)
{
Logger.Error(“Batch- “+ Batch ID +”; Job-”+ context.BackgroundJob.Id + " has been failed due to an exception " + failedState.Exception + "
")}
}
}
Thanks