Skip to content
ConsoleMonster

Hosting dashboard

ConsoleMonster.Hosting adds a live dashboard to a .NET generic host: each hosted service's state, failures and the log.

Add the dashboard and the logging provider to an existing host:

dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging

The dashboard depends only on Microsoft.Extensions.Hosting.Abstractions and ConsoleMonster.

A generic host with a live dashboard

Three hosted services: a queue worker, a cache warmer that completes and a report builder that fails. Log lines go into the dashboard's log region.

dotnet run 110×3024-bitFull
dotnet new console -n Orders
cd Orders
dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging
dotnet add package Microsoft.Extensions.Hosting
PowerShellRun.ps1View on GitHub
# … 9 lines of help in the full program
[CmdletBinding()]
param([string] $Project = '.')

& dotnet run --project $Project -c Release
if ($LASTEXITCODE -ne 0) { throw "The .NET example exited with code $LASTEXITCODE." }
Full program
<#
.SYNOPSIS
Runs the .NET integration project from PowerShell.
.DESCRIPTION
Create the project and save Program.cs from either C# tab first. The .NET host and logging provider own this lifecycle;
there is no separate PowerShell hosting implementation.
.PARAMETER Project
The project file or directory containing the saved C# program (default current directory).
#>
[CmdletBinding()]
param([string] $Project = '.')

& dotnet run --project $Project -c Release
if ($LASTEXITCODE -ne 0) { throw "The .NET example exited with code $LASTEXITCODE." }

Run: ./Run.ps1

PowerShell launches the same .NET project after Program.cs is saved from either C# tab. The generic host and ILogger provider have no separate cmdlet implementation.

Terminal
dotnet new console -n Orders
cd Orders
dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging
dotnet add package Microsoft.Extensions.Hosting
C#Program.csView on GitHub
// A generic host with three workers and the console dashboard; log entries go into its log region. Ctrl+C stops it.
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
{
    ApplicationName = "Orders",
    EnvironmentName = "Staging"
});
builder.Logging.ClearProviders();
builder.Logging.AddConsoleMonster();

// A worker that fails shows as failed on the dashboard while the others keep running.
builder.Services.Configure<HostOptions>(
    options => options.BackgroundServiceExceptionBehavior =
        BackgroundServiceExceptionBehavior.Ignore);
builder.Services.AddHostedService<QueueWorker>();
builder.Services.AddHostedService<CacheWarmer>();
builder.Services.AddHostedService<ReportBuilder>();
builder.Services.AddConsoleDashboard(dashboard => dashboard.LogHeight = 6);
await builder.Build().RunAsync();

sealed class QueueWorker(ILogger<QueueWorker> log,
    IConsoleDashboard dashboard) : BackgroundService {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        int order = 1040;
        while (!stoppingToken.IsCancellationRequested) {
            order++;
            dashboard.SetDetail("Queue", (50 - order % 50) + " waiting");
            log.LogInformation("Picked up order {Order}", order);
            if (order % 4 == 0) {
                log.LogWarning("Payment slow for order {Order}", order);
            }
            try {
                await Task.Delay(900, stoppingToken);
            } catch (OperationCanceledException) {
                break;
            }
        }
    }
}

sealed class CacheWarmer(ILogger<CacheWarmer> log) : BackgroundService {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        await Task.Delay(2500, stoppingToken);
        log.LogInformation("Cache warmed: {Entries} entries", 12840);
    }
}

sealed class ReportBuilder(ILogger<ReportBuilder> log) : BackgroundService {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        await Task.Delay(4000, stoppingToken);
        log.LogError("The report template folder is missing");
        throw new InvalidOperationException("The report template folder is missing.");
    }
}
Full program
using ConsoleMonster.Extensions.Logging;
using ConsoleMonster.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

// A generic host with three workers and the console dashboard; log entries go into its log region. Ctrl+C stops it.
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
{
    ApplicationName = "Orders",
    EnvironmentName = "Staging"
});
builder.Logging.ClearProviders();
builder.Logging.AddConsoleMonster();

// A worker that fails shows as failed on the dashboard while the others keep running.
builder.Services.Configure<HostOptions>(
    options => options.BackgroundServiceExceptionBehavior =
        BackgroundServiceExceptionBehavior.Ignore);
builder.Services.AddHostedService<QueueWorker>();
builder.Services.AddHostedService<CacheWarmer>();
builder.Services.AddHostedService<ReportBuilder>();
builder.Services.AddConsoleDashboard(dashboard => dashboard.LogHeight = 6);
await builder.Build().RunAsync();

sealed class QueueWorker(ILogger<QueueWorker> log,
    IConsoleDashboard dashboard) : BackgroundService {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        int order = 1040;
        while (!stoppingToken.IsCancellationRequested) {
            order++;
            dashboard.SetDetail("Queue", (50 - order % 50) + " waiting");
            log.LogInformation("Picked up order {Order}", order);
            if (order % 4 == 0) {
                log.LogWarning("Payment slow for order {Order}", order);
            }
            try {
                await Task.Delay(900, stoppingToken);
            } catch (OperationCanceledException) {
                break;
            }
        }
    }
}

sealed class CacheWarmer(ILogger<CacheWarmer> log) : BackgroundService {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        await Task.Delay(2500, stoppingToken);
        log.LogInformation("Cache warmed: {Entries} entries", 12840);
    }
}

sealed class ReportBuilder(ILogger<ReportBuilder> log) : BackgroundService {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        await Task.Delay(4000, stoppingToken);
        log.LogError("The report template folder is missing");
        throw new InvalidOperationException("The report template folder is missing.");
    }
}

Run: dotnet run · Ctrl+C stops the host.

Terminal
dotnet new console -n Orders
cd Orders
dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging
dotnet add package Microsoft.Extensions.Hosting
C# fluentProgram.cs
// A generic host with three workers and the console dashboard; log entries go into its log region. Ctrl+C stops it.
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
{
    ApplicationName = "Orders",
    EnvironmentName = "Staging"
});
builder.Logging.ClearProviders();
builder.Logging.AddConsoleMonster();

// A worker that fails shows as failed on the dashboard while the others keep running.
builder.Services.Configure<HostOptions>(
    options => options.BackgroundServiceExceptionBehavior =
        BackgroundServiceExceptionBehavior.Ignore);
builder.Services.AddHostedService<QueueWorker>();
builder.Services.AddHostedService<CacheWarmer>();
builder.Services.AddHostedService<ReportBuilder>();
builder.Services.AddConsoleDashboard(dashboard => dashboard.LogHeight(6));
await builder.Build().RunAsync();

sealed class QueueWorker(ILogger<QueueWorker> log, IConsoleDashboard dashboard) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        int order = 1040;
        while (!stoppingToken.IsCancellationRequested)
        {
            order++;
            dashboard.SetDetail("Queue", (50 - order % 50) + " waiting");
            log.LogInformation("Picked up order {Order}", order);
            if (order % 4 == 0)
            {
                log.LogWarning("Payment slow for order {Order}", order);
            }

            try
            {
                await Task.Delay(900, stoppingToken);
            }
            catch (OperationCanceledException)
            {
                break;
            }
        }
    }
}

sealed class CacheWarmer(ILogger<CacheWarmer> log) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Delay(2500, stoppingToken);
        log.LogInformation("Cache warmed: {Entries} entries", 12840);
    }
}

sealed class ReportBuilder(ILogger<ReportBuilder> log) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Delay(4000, stoppingToken);
        log.LogError("The report template folder is missing");
        throw new InvalidOperationException("The report template folder is missing.");
    }
}
Full program
using ConsoleMonster.Extensions.Logging;
using ConsoleMonster.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

// A generic host with three workers and the console dashboard; log entries go into its log region. Ctrl+C stops it.
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
{
    ApplicationName = "Orders",
    EnvironmentName = "Staging"
});
builder.Logging.ClearProviders();
builder.Logging.AddConsoleMonster();

// A worker that fails shows as failed on the dashboard while the others keep running.
builder.Services.Configure<HostOptions>(
    options => options.BackgroundServiceExceptionBehavior =
        BackgroundServiceExceptionBehavior.Ignore);
builder.Services.AddHostedService<QueueWorker>();
builder.Services.AddHostedService<CacheWarmer>();
builder.Services.AddHostedService<ReportBuilder>();
builder.Services.AddConsoleDashboard(dashboard => dashboard.LogHeight(6));
await builder.Build().RunAsync();

sealed class QueueWorker(ILogger<QueueWorker> log, IConsoleDashboard dashboard) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        int order = 1040;
        while (!stoppingToken.IsCancellationRequested)
        {
            order++;
            dashboard.SetDetail("Queue", (50 - order % 50) + " waiting");
            log.LogInformation("Picked up order {Order}", order);
            if (order % 4 == 0)
            {
                log.LogWarning("Payment slow for order {Order}", order);
            }

            try
            {
                await Task.Delay(900, stoppingToken);
            }
            catch (OperationCanceledException)
            {
                break;
            }
        }
    }
}

sealed class CacheWarmer(ILogger<CacheWarmer> log) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Delay(2500, stoppingToken);
        log.LogInformation("Cache warmed: {Entries} entries", 12840);
    }
}

sealed class ReportBuilder(ILogger<ReportBuilder> log) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Delay(4000, stoppingToken);
        log.LogError("The report template folder is missing");
        throw new InvalidOperationException("The report template folder is missing.");
    }
}

Run: dotnet run · Ctrl+C stops the host.

What it shows

  • A header with the title (default: the application name), the environment and the elapsed time.
  • One line per hosted service registered before AddConsoleDashboard, with its state (waiting, starting, running, stopping, stopped, completed or failed), how long it has been in it and the failure message when it fails. A BackgroundService shows completed when ExecuteAsync returns and failed when it throws.
  • Your own entries through IConsoleDashboard, which you can inject anywhere: SetStatus("Import", ServiceState.Running, "file 3 of 10"), SetDetail, Log and Snapshot.
  • A log region that ConsoleMonster.Extensions.Logging writes into while the dashboard shows. Remove the default console logger (builder.Logging.ClearProviders()) first, so it does not print over the view.

When the host stops, the dashboard stops last and leaves its final state in the scrollback. Ctrl+C stays with the host.

When it stays out of the way

With Mode = Auto (the default) the dashboard draws only on an interactive terminal: not when output is redirected, in CI or a container log, in a Windows service or a daemon without a terminal. There the host and its logging run exactly as they would without it. Always draws whenever the terminal can, and Never only tracks the statuses for Snapshot().

OptionDefaultMeaning
Titleapplication nameThe header title.
ModeAutoAuto, Always or Never.
ShowServicestrueLists the hosted services and your entries.
ShowLogtrueShows the log region.
LogHeight8Rows of the log region.
KeepFinalStatetrueLeaves the last frame in the scrollback.
ServiceNametype nameNames a hosted service from its type.

Next

Edit this page on GitHub