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.
12:00:00 ✗ ERR Host BackgroundService failed ✗ InvalidOperationException The report template folder is missing. at ReportBuilder.ExecuteAsync(CancellationToken stoppingToken) Program.cs:57 + 1 framework frame Orders · Staging ctrl+c stops · 00:00:09 ● Queue running 9.0s 50 waiting ✓ CacheWarmer completed 2.5s ✗ ReportBuilder failed 4.0s InvalidOperationException: The report template folder is missing. ── log · times since 12:00:00 ─────────────────────────────────────────────────────────────────────────────── 00:04 QueueWorker Picked up order 1046 00:05 QueueWorker Picked up order 1047 00:06 QueueWorker Picked up order 1048 00:06 ▲ WARN QueueWorker Payment slow for order 1048 00:07 QueueWorker Picked up order 1049 00:08 QueueWorker Picked up order 1050
dotnet new console -n Orders
cd Orders
dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging
dotnet add package Microsoft.Extensions.Hosting# … 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.
dotnet new console -n Orders
cd Orders
dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging
dotnet add package Microsoft.Extensions.Hosting// 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.
dotnet new console -n Orders
cd Orders
dotnet add package ConsoleMonster.Hosting
dotnet add package ConsoleMonster.Extensions.Logging
dotnet add package Microsoft.Extensions.Hosting// 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.
See also: Hosting dashboard
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. ABackgroundServiceshows completed whenExecuteAsyncreturns and failed when it throws. - Your own entries through
IConsoleDashboard, which you can inject anywhere:SetStatus("Import", ServiceState.Running, "file 3 of 10"),SetDetail,LogandSnapshot. - A log region that
ConsoleMonster.Extensions.Loggingwrites 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().
| Option | Default | Meaning |
|---|---|---|
Title | application name | The header title. |
Mode | Auto | Auto, Always or Never. |
ShowServices | true | Lists the hosted services and your entries. |
ShowLog | true | Shows the log region. |
LogHeight | 8 | Rows of the log region. |
KeepFinalState | true | Leaves the last frame in the scrollback. |
ServiceName | type name | Names a hosted service from its type. |
Next
- Recording and replay: record a session and export a player or GIF.
- Logging: the provider that writes into the dashboard's log region.
- Threads and concurrency: how updates reach the screen.