Hangfire (v1.3 +) has a 'clever' feature where it picks up your application's existing logging setup and uses it.
Starting from Hangfire 1.3.0, you are not required to do anything, if your application already uses one of the following libraries through the reflection (so that Hangfire itself does not depend on any of them).
Because I don't want hangfire logging mixed in with my application logs I would like to filter them out into a separate log file.
Serilog has filters to do this, but it needs something to filter on.
Does Hangfire include any useful context that I can specify when filtering?
I couldn't get the Serilog Matching.FromSource(...)
to work, the Hangfire events don't appear to have that property. I have the following solution:
var logFile = "...";
var hangfireFile = "...";
var hangfireEvents = Matching.WithProperty<string>("Name", x => x.StartsWith("Hangfire"));
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Logger(lc =>
lc.Filter.ByExcluding(hangfireEvents)
.WriteTo.RollingFile(new CompactJsonFormatter(new SafeJsonFormatter()), logFile))
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(hangfireEvents)
.WriteTo.RollingFile(new CompactJsonFormatter(new SafeJsonFormatter()), hangfireFile))
.CreateLogger();