Hide certain jobs in dashboard-log

Bit of a late reply but here goes.

I struggled with a similar situation. What I ended up doing is changing the return value from a job’s Run method to string (as opposed to void).

Hangfire already supports this out of the box. In the job itself I simply record if the job did anything useful (my jobs check for new files to download from an sftp, if there is no new file the job returns null, if there is I return a string like “1 file downloaded”).

The ‘return’ value (either the string or a null) will be saved by hangfire as the job ‘Result’ field.

So far so good. The trouble is there is no way to only see succeeded jobs with Result != null field. So what I ended up doing is a copy and paste of the Succeeded job page code (as is in the Hangfire code) and change it slightly to only show succeeded jobs based on the Result. This new page is then added to the standard pages as a custom page.

Some snippets of code:

The Run method of one of my jobs :

public string Run(PerformContext context, string backgroundJobId, string tag)
{
… your code …
return result;
}

In the copied Succeed jobs page (SucceededJobs1.generated.cs) there is this line :

var succeededJobs = monitor.SucceededJobs(pager.FromRecord, pager.RecordsPerPage);

What that does is load a block of jobs depending on the pager (i.e. how many jobs to see per page like 10 or 20)

All I did was change that line to this :

var succeededJobs = monitor.SucceededJobs(0, 50000).Where(x => x.Value.Result != null);

As you can see it’s a bit of a kludge, since I cannot use the pager function easily. The pager depends on the storage module telling it the count of succeeded jobs, there is no function that allows you to get a count for succeeded jobs where result != null. So my change takes a rather large number of results, at some point I will probably fix that and add said count function to the storage module. For now this works for me however as the overall number of jobs that actually did something is relatively small.

All that is left is to tell the dashboard about this custom page so in the startup of my dashboard apphost at the back (after setting up the dashboard) I have something like this :

       app.UseHangfireDashboard("/hangfire", options);
       DashboardRoutes.Routes.AddRazorPage("/jobs/filtered", x => new FilteredPage());
        JobsSidebarMenu.Items.Add(
            rp =>
            {
                var url = "/hangfire/jobs/filtered";
                return new MenuItem("Filtered", url);
            });

With FilteredPage being the copy I made of the Succeeded job page.

The result is an additional menu option in the sidebar menu of the dashboard that leads you to the new page with just jobs that have a Result :

Filtered