Enriching Serilog logs with JobId using a Filter

Tags: #<Tag:0x00007f7b5d279ce8> #<Tag:0x00007f7b5c1d00e0>

I’m using Serilog for logging in the system and I want every single log message produced as part of the processing of a message to include the JobId. I have something working but I would like to confirm this is the ideal approach?

public class JobContext : IServerFilter
    {
        public void OnPerformed(PerformedContext filterContext)
        {
            _contextualLogger?.Dispose();
        }

        public void OnPerforming(PerformingContext filterContext)
        {
            JobId = filterContext.BackgroundJob.Id;
            _contextualLogger = Serilog.Context.LogContext.PushProperty("JobId", JobId, true)
        }

        public static string JobId
        {
            get { return _jobId; }
            set { _jobId = value; }
        }

        [ThreadStatic]
        private static string _jobId;

        [ThreadStatic]
        private static IDisposable _contextualLogger;
    }

And here is how I initialize Hangfire:

GlobalConfiguration.Configuration.UseSqlServerStorage(
    () => new Microsoft.Data.SqlClient.SqlConnection("conn_string"), hangfireOptions)
    .UseAutofacActivator(container)
    .UseFilter(new JobContext())
    .UseBatches();

Versions involved:

.NET 4.7
Hangfire.Core 1.7