I am trying to add the Hangfire JobId (and other properties) to all of my jobs without having to add it to each job method.
In Startup.cs I added a promising filter solution however I don't know how to apply it to my test method and I am not even sure if this is the right way to do it.
Startup.cs
...
GlobalJobFilters.Filters.Add(new JobLoggerAttribute());
JobLoggerAttribute class - from here: Link
The task for me is to put the JobId where the column cell is currently blank without having to add it to my Test method manually:

Program.cs (globally defined Logger):
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.Elasticsearch....
.Enrich.FromLogContext()
.CreateLogger();
Because I also want to retrieve method name etc. I added this class to call the Here method in my jobs.
LoggerExtensions.cs:
public static class LoggerExtensions
{
public static ILogger Here(this ILogger logger,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
return logger
.ForContext("MemberName", memberName)
.ForContext("FilePath", sourceFilePath)
.ForContext("LineNumber", sourceLineNumber)
;
}
}
RunTestTask:
public void RunTestTask(PerformContext context)
{
// instead of having to write this line in all methods
// LogContext.PushProperty("JobId", context.BackgroundJob.Id);
Log.Logger.Here().Information(string.Format("Serilog: Hanfire Job {1} Scheduled at {0}", DateTime.Now, context.BackgroundJob.Id));
}
Job Enqueue:
BackgroundJob.Enqueue(() => new Jobs.Test.TestService().RunTestTask(null));
Hoping for tips and tricks.
Thank you.