Hangfire.Console Documentation

repository·master·Indexed 19 days ago

https://github.com/pieceofsummer/hangfire.console

A library providing a real-time, console-like logging experience and progress bars directly within the Hangfire Dashboard. It allows developers to view job logs and execution progress via PerformContext, featuring colored text, customizable console options, and a WithProgress extension method for tracking enumeration progress.

Tokens
1.2K
Snippets
4
Records
5
Agent score
17%

What's inside Hangfire.Console

  1. Install and Setup Hangfire.Console

    master

    Hangfire.Console provides a console-like logging experience for your Hangfire jobs. To enable it, call .UseConsole() during your Hangfire configuration.

    Important: If your Hangfire Dashboard and Hangfire Server are running in separate processes, you must call .UseConsole() on both.

    Configuration via .NET Core Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHangfire(config =>
        {
            config.UseSqlServerStorage("connectionSting");
            config.UseConsole();
        });
    }

    Configuration via GlobalConfiguration

    GlobalConfiguration.Configuration
        .UseSqlServerStorage("connectionSting")
        .UseConsole();

    Note: If you change configuration options or initially add the library, you may need to clear your browser cache to ensure the generated CSS/JS is updated.

  2. Configure Hangfire.Console options

    master

    You can pass an options object to the UseConsole() method to customize the console behavior.

    OptionDescription
    ExpireInTime to keep console sessions (default: 24 hours)
    FollowJobRetentionPolicyExpire all console sessions along with parent job (default: true)
    PollIntervalPolling interval for live updates in milliseconds (default: 1000)
    BackgroundColorConsole background color (default: #0d3163)
    TextColorConsole default text color (default: #ffffff)
    TimestampColorTimestamp text color (default: #00aad7)
  3. Track enumeration progress with WithProgress

    master

    The library provides a WithProgress extension method to automatically update a progress bar while iterating over a collection.

    Usage

    1. Create a progress bar using context.WriteProgressBar().
    2. Wrap your collection in the foreach loop using .WithProgress(bar).

    Key Behaviors

    • Automatic Updates: The progress bar updates automatically during iteration.
    • Interruption Handling: If the loop is exited early via a break instruction, the progress bar is automatically set to 100%.
    • Count Determination: If the collection does not implement ICollection, ICollection<T>, or IReadOnlyCollection<T>, you must manually provide the count argument to the extension method so progress can be calculated.
    public void TaskMethod(PerformContext context)
    {
        var bar = context.WriteProgressBar();
        
        foreach (var item in collection.WithProgress(bar))
        {
            // do work
        }
    }
  4. Log messages using PerformContext

    master

    To log messages to the Hangfire console, you must include PerformContext as an argument in your job method. Hangfire will automatically substitute this argument at runtime.

    Note: When enqueuing the job manually, pass null for the PerformContext parameter.

    Basic Logging

    Use context.WriteLine("message") to write text to the console.

    Colored Text

    You can change the text color using context.SetTextColor(ConsoleTextColor.Color) and revert it with context.ResetTextColor().

    public void TaskMethod(PerformContext context)
    {
        context.WriteLine("Hello, world!");
    }
    
    // Colored logging
    public void TaskMethod(PerformContext context)
    {
        context.SetTextColor(ConsoleTextColor.Red);
        context.WriteLine("Error!");
        context.ResetTextColor();
    }
  5. Use progress bars in jobs

    master

    You can create and update inline progress bars within your jobs using PerformContext.WriteProgressBar().

    • Initialization: By default, a progress bar starts at 0. You can specify an initial value and a color as optional arguments in WriteProgressBar().
    • Updating: Use the SetValue(value) method on the returned progress bar object to update its progress.
    • Multiple Bars: You can create and manage multiple progress bars independently within a single job.
    public void TaskMethod(PerformContext context)
    {
        // create progress bar
        var progress = context.WriteProgressBar();
        
        // update value for previously created progress bar
        progress.SetValue(100);
    }