I’d like to add messages generated by the execution of a Job and display them in the JobDetailsPage. I’m thinking to something like a job contextualized log, something that reports in a human readable form what the job did.
I’ve found that I can add a parameter to the method that I’m scheduling and set thata parameter from an IServerFilter, in order to supply the JobId, but I’m not sure I’m on the right path.
Can anyone share his thoughts ?
I’m looking into this myself, but since i’m quite new to Hangfire myself i can’t tell if its the right way to go or no.
I’ve so far implemented it the same way as you described with a separate log table where per-job logging goes into. I haven’t gotten to extending the dashboard yet and i’m not sure i will since i’m in the process of building up a custom view of executed jobs using the IMonitoringApi.
i’m really having a hard time getting this jobid into the running task. this does not work as expected,
putting the SetJobParameter code in OnCreated is too late as the task has already been instantiated.
how did you mange?
here is the attribute i’m using
public class CaptureJobIdAttribute : JobFilterAttribute, IClientFilter, IServerFilter
{
public void OnCreating(CreatingContext filterContext)
{
if (filterContext == null) throw new ArgumentNullException("filterContext");
filterContext.SetJobParameter(
"jobId", filterContext.JobId); // jobId is still empty here
}
using it like this
[Queue("demo")]
[CaptureJobId]
public void Execute(IJobCancellationToken token, string jobId)
{
_log.Info("rb.logging says: EXECUTE " + jobId);
and queued like this:
RecurringJob.AddOrUpdate<DemoJob>(x => x.Execute(JobCancellationToken.Null, string.Empty), Cron.Hourly);
Hi … for reference, I’ve included a Github issue with some logging discussion…