Show job logs on the dashboard.
The project takes advantage of the integration with NLog thanks to NLog.HangfireJobLogsTarget.
Note: the project is really simple but it is a good starting point (in my opinion).
Hangfire.Dashboard.JobLogs is available as a NuGet package. You can install it using the NuGet Package Console window:
PM> Install-Package Bonura.Hangfire.Dashboard.JobLogs
NLog configuration with HangfireJobLogs
target and layout with ${hangfire-decorator}
(mandatory).
"NLog": {
"extensions": [
{
"assembly": "NLog.HangfireJobLogsTarget"
}
],
"default-wrapper": {
"type": "AsyncWrapper",
"overflowAction": "Block"
},
"targets": {
"hangfire_dashboard": {
"layout": "${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=toString}${hangfire-decorator}",
"type": "HangfireJobLogs"
}
},
"rules": {
"hangfire": {
"logger": "Sample.Jobs.*",
"minLevel": "Info",
"writeTo": "hangfire_dashboard"
}
}
}
Services configuration:
// Add hangfire context services
builder.Services.AddHangfirePerformContextAccessor();
// Add Hangfire services.
builder.Services.AddHangfire((serviceProvider, config) =>
{
// Add filter to handle PerformContextAccessor
config.UsePerformContextAccessorFilter(serviceProvider);
// Add a storage
config.UseInMemoryStorage();
// Add dashboard jobLogs
config.UseDashboardJobLogs();
});
// Add the processing server as IHostedService
builder.Services.AddHangfireServer();
Job using ILogger
(Microsoft.Extensions.Logging):
public class SimpleJob
{
private ILogger<SimpleJob> _logger;
public SimpleJob(ILogger<SimpleJob> logger)
{
_logger = logger;
}
public async Task DoJobAsync()
{
_logger.LogInformation("Log message");
await Task.CompletedTask;
}
}
Basically, a log message produced by a hangfire job is captured by NLog NLog.HangfireJobLogsTarget
. Thanks to ${hangfire-decorator}
and PerformContextAccessor
log messages are sent to Hangfire storage.
Finally, log messages are shown on the detail page thanks to a simple JobDetailsRenderer
.
See sample project:
https://github.com/meriturva/Hangfire.Dashboard.JobLogs/tree/main/Sample