Skip to content
A Hangfire extension to store a log during job execution.
C# HTML ASP
Branch: master
Clone or download
Latest commit 3f79dec Dec 4, 2019

README.md

Hangfire.JobsLogger

NuGet Actions Status Official Site License MIT

Overview

A Hangfire extension to store a log during job execution

joblogshistory

Build Status

Platform Master Develop
Windows Build Status Build Status
Linux / Mac OS Build Status Build Status

Installation

Install a package from Nuget.

Install-Package Hangfire.JobsLogger

Usage

DotNetCore

For service side:

services.AddHangfire(config => config.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))
                                                 .UseJobsLogger();

NetFramework

For startup side:

GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnection").UseJobsLogger();

Example

RecurringJob.AddOrUpdate(() => taskExample.TaskMethod(null), Cron.Minutely);

//...

private readonly ILogger _log = ApplicationLogging.CreateLogger<TaskExample>();

public void TaskMethod(PerformContext context)
{
  var jobId = context.BackgroundJob.Id;

  foreach (int i in Enumerable.Range(1, 10)) 
  {
    context.LogTrace($"{i} - Trace Message.. {DateTime.UtcNow.Ticks}");
    context.LogDebug($"{i} - Debug Message.. {DateTime.UtcNow.Ticks}");
    context.LogInformation($"{i} - Information Message.. {DateTime.UtcNow.Ticks}");
    context.LogWarning($"{i} - Warning Message.. {DateTime.UtcNow.Ticks}");
    context.LogError($"{i} - Error Message.. {DateTime.UtcNow.Ticks}");
    context.LogCritical($"{i} - Critical Message.. {DateTime.UtcNow.Ticks}");

    //Traditional ILogger Usage
    _log.LogTrace(jobId, $"{i} - Trace Message.. {DateTime.UtcNow.Ticks}");
    _log.LogDebug(jobId, $"{i} - Debug Message.. {DateTime.UtcNow.Ticks}");
    _log.LogInformation(jobId, $"{i} - Information Message.. {DateTime.UtcNow.Ticks}");
    _log.LogWarning(jobId, $"{i} - Warning Message.. {DateTime.UtcNow.Ticks}");
    _log.LogError(jobId, $"{i} - Error Message.. {DateTime.UtcNow.Ticks}");
    _log.LogCritical(jobId, $"{i} - Critical Message.. {DateTime.UtcNow.Ticks}");
  }
}

Note Hangfire is responsible for injecting an instance of the PerformContext class.

The logs can be consulted in the detail of the logs on the enqueued state card: jobdetail

Options

In the UseJobsLogger method you can use an instance of the Hangfire.JobsLogger.JobsLoggerOptions class to specify some options of this plugin.

Below is a description of them:

Option Description Default Value
LogLevel Set the log level to be stored in hangfire Microsoft.Extensions.Logging.LogLevel.Trace
LogTraceColor Color that will be used to display log messages of this type System.Drawing.Color.LightGreen
LogDebugColor Color that will be used to display log messages of this type System.Drawing.Color.DarkGreen
LogInformationColor Color that will be used to display log messages of this type System.Drawing.Color.Blue
LogWarningColor Color that will be used to display log messages of this type System.Drawing.Color.DarkOrange
LogErrorColor Color that will be used to display log messages of this type System.Drawing.Color.Red
LogCriticalColor Color that will be used to display log messages of this type System.Drawing.Color.DarkRed

Credits

  • Brayan Mota
  • Lucas Ferreras

Thanks

This project would not have been possible, without the collaboration of the following projects:

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

paypal

License

This project is under MIT license. You can obtain the license copy here.

You can’t perform that action at this time.