IdentityStream.Hangfire.Console 2.0.0

IdentityStream.Hangfire.Console

Build Status NuGet MIT License

Inspired by AppVeyor, Hangfire.Console provides a console-like logging experience for your jobs.

[!IMPORTANT] As Hangfire.Console seems to be abandoned, this is a fork with some crucial bugfixes. If, at some point in the future, the main project comes back to life, these fixes should probably be merged back upstream.

dashboard

Features

  • Provider-agnostic: (allegedly) works with any job storage provider (currently tested with SqlServer and MongoDB).
  • 100% Safe: no Hangfire-managed data (e.g. jobs, states) is ever updated, hence there's no risk to corrupt it.
  • With Live Updates: new messages will appear as they're logged, as if you're looking at real console.
  • (blah-blah-blah)

Setup

In .NET Core's Startup.cs:

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

Otherwise,

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

NOTE: If you have Dashboard and Server running separately, you'll need to call UseConsole() on both.

Additional options

As usual, you may provide additional options for UseConsole() method.

Here's what you can configure:

  • ExpireIn – time to keep console sessions (default: 24 hours)
  • FollowJobRetentionPolicy – expire all console sessions along with parent job (default: true)
  • PollInterval – poll interval for live updates, ms (default: 1000)
  • BackgroundColor – console background color (default: #0d3163)
  • TextColor – console default text color (default: #ffffff)
  • TimestampColor – timestamp text color (default: #00aad7)

NOTE: After you initially add Hangfire.Console (or change the options above) you may need to clear browser cache, as generated CSS/JS can be cached by browser.

Log

Hangfire.Console provides extension methods on PerformContext object, hence you'll need to add it as a job argument.

NOTE: Like IJobCancellationToken, PerformContext is a special argument type which Hangfire will substitute automatically. You should pass null when enqueuing a job.

Now you can write to console:

public void TaskMethod(PerformContext context)
{
    context.WriteLine("Hello, world!");
}

Like with System.Console, you can specify text color for your messages:

public void TaskMethod(PerformContext context)
{
    context.SetTextColor(ConsoleTextColor.Red);
    context.WriteLine("Error!");
    context.ResetTextColor();
}

Progress bars

Version 1.1.0 adds support for inline progress bars:

progress

public void TaskMethod(PerformContext context)
{
    // create progress bar
    var progress = context.WriteProgressBar();
    
    // update value for previously created progress bar
    progress.SetValue(100);
}

You can create multiple progress bars and update them separately.

By default, progress bar is initialized with value 0. You can specify initial value and progress bar color as optional arguments for WriteProgressBar().

Enumeration progress

To easily track progress of enumeration over a collection in a for-each loop, library adds an extension method WithProgress:

public void TaskMethod(PerformContext context)
{
    var bar = context.WriteProgressBar();
    
    foreach (var item in collection.WithProgress(bar))
    {
        // do work
    }
}

It will automatically update progress bar during enumeration, and will set progress to 100% if for-each loop was interrupted with a break instruction.

NOTE: If the number of items in the collection cannot be determined automatically (e.g. collection doesn't implement ICollection/ICollection<T>/IReadOnlyCollection<T>, you'll need to pass additional argument count to the extension method).

License

Copyright (c) 2016 Alexey Skalozub
Copyright (c) 2023 IdentityStream AS

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Showing the top 20 packages that depend on IdentityStream.Hangfire.Console.

Packages Downloads
Hangfire.Console.Extensions
Makes it easier to use Hangfire.Console with .net core dependency injection
4
Hangfire.Console.Extensions.Serilog
Makes it easier to use Hangfire.Console with serilog
6

History: v2.0.0: • Changed target framework to .NET Standard 2.0 • Bumped Hangfire.Core dependency to latest version • Calling UseConsole more than once will no longer throw • Fixed number formatting/parsing bug causing console to poll forever • Added digits argument to progress bar, defaulting to 0 decimal digits - This prevents huge amounts of progress bar updates with fractional digit changes v1.4.2: • Added StringFormatMethod attributes on WriteLine methods (for better intellisense) v1.4.1: • Fix job progress style • Use explicit json serializer settings • Remove ConsoleContext from Items in OnPerformed v1.4.0: • Show job progress at Processing Jobs page v1.3.10: • Fix expiration issues (#47) v1.3.9: • Relax Newtonsoft.Json dependency version for .NET 4.5 v1.3.8: • Fix WriteLine thread-safety issues v1.3.7: • Prevent calling UseConsole() twice • Collapse outdated consoles v1.3.6: • Make progress bars' SetValue thread-safe • Add support for named progress bars v1.3.5: • Add more overloads for WriteLine and WithProgress extension methods v1.3.4: • Fixed hyperlink detection for urls with query string parameters (#37) • Fixed loading dots indicator position on tiny screens v1.3.3: • Eliminated unnecessary state filter executions v1.3.2: • Fixed console expiration for some storages (e.g. Hangfire.Redis.StackExchange) v1.3.1: • Fixed compatibility with Hangfire 1.6.11+ v1.3.0: • Consoles are now expired along with parent job by default! • Added **FollowJobRetentionPolicy** option to switch between old/new expiration modes v1.2.1: • Added Monitoring API v1.2.0: • Added hyperlink detection v1.1.7: • Fixed line ordering issue v1.1.6: • Changed key format to support single-keyspace storages, like Hangfire.Redis v1.1.5: • Allow WriteLine/WriteProgressBar calls with a null PerformContext v1.1.4: • Added support of fractional progress values • Added WithProgress() extension methods for tracking enumeration progress in for-each loops v1.1.3: • Fixed ugly font on OS X • Fixed animation lags on all major browsers v1.1.2: • Added support for long messages • Refactor for better testability v1.1.1: • Don't show current console while there's no lines v1.1.0: • Added progress bars v1.0.2: • Added some more configuration options • Fixed occasional duplicate lines collapsing v1.0.1: • Fixed compatibility issues with storages losing DateTime precision (like MongoDB) • Improved client-side experience v1.0.0: • Initial release

.NET Standard 2.0

Version Downloads Last updated
2.0.0 6 15.02.2024