Skip to content
ConsoleMonster

dotnet new template

dotnet new consolemonster: a console app with a menu, a status report, logging, the hosting dashboard and native AOT.

dotnet new consolemonster creates a console app with a menu, a status report, logging, the hosting dashboard and native AOT publishing, ready to change into your own tool.

  1. Install the template (it needs the .NET 8 SDK or later):

    dotnet new install ConsoleMonster.Templates
    
  2. Create an app and start it. dotnet run opens the menu; dotnet run -- serve starts the service with its dashboard, and Ctrl+C stops it.

    dotnet new consolemonster -o MyTool
    cd MyTool
    dotnet run
    
  3. Publish one native executable. Native AOT needs the C++ build tools: on Windows, Visual Studio's "Desktop development with C++" workload.

    dotnet publish -c Release -r win-x64
    

dotnet new consolemonster

The template's app: a menu with a status report and a page, then dotnet run -- serve with the hosting dashboard until Ctrl+C.

dotnet run, then dotnet run -- serve 100×2624-bitFull
PowerShelltemplate.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: ./template.ps1

PowerShell launches the same .NET application after Program.cs is saved from either C# tab.

C#Program.csView on GitHub
public static async Task<int> Main(string[] args) {
    if (args.Length > 0 && args[0] == "serve") {
        await Service.RunAsync(args).ConfigureAwait(false);
        return 0;
    }
    if (Terminal.Profile.Mode != OutputMode.Interactive) {
        // Redirected, CI or a pipe: the report as text, no menu.
        Terminal.Write(Report.Build());
        return 0;
    }
    Menu menu = new Menu("ConsoleMonsterApp") { Description = "Pick an item with the arrow keys and Enter; Esc goes back." };
    menu.Item("Status report", item => {
        item.Description = "A table of services and a summary";
        item.Page = _ => Report.Build();
    });
    menu.Item("Run the service", item => {
        item.Description = "How to start the host with its live dashboard";
        item.Page = _ => new Panel(TextBlock.FromMarkup(
            "Run [accent]dotnet run -- serve[/] to start the generic host. The dashboard shows each hosted service, "
            + "its state and the log; Ctrl+C stops it.\n\nPublish one native executable with "
            + "[accent]dotnet publish -c Release -r win-x64[/] (or linux-x64, osx-arm64)."), "Service");
    });
    menu.Exit();
    menu.Run();
    return 0;
}
Full program
using ConsoleMonster;
using ConsoleMonster.Capabilities;
using ConsoleMonster.Extensions.Logging;
using ConsoleMonster.Hosting;
using ConsoleMonster.Interactive;
using ConsoleMonster.Layout;
using ConsoleMonster.Styling;
using ConsoleMonster.Widgets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ConsoleMonsterApp;

/// <summary>
/// `dotnet run` opens the menu (or prints the report when the output is redirected);
/// `dotnet run -- serve` runs the generic host with the live console dashboard until Ctrl+C.
/// </summary>
public static class Program {
    public static async Task<int> Main(string[] args) {
        if (args.Length > 0 && args[0] == "serve") {
            await Service.RunAsync(args).ConfigureAwait(false);
            return 0;
        }
        if (Terminal.Profile.Mode != OutputMode.Interactive) {
            // Redirected, CI or a pipe: the report as text, no menu.
            Terminal.Write(Report.Build());
            return 0;
        }
        Menu menu = new Menu("ConsoleMonsterApp") { Description = "Pick an item with the arrow keys and Enter; Esc goes back." };
        menu.Item("Status report", item => {
            item.Description = "A table of services and a summary";
            item.Page = _ => Report.Build();
        });
        menu.Item("Run the service", item => {
            item.Description = "How to start the host with its live dashboard";
            item.Page = _ => new Panel(TextBlock.FromMarkup(
                "Run [accent]dotnet run -- serve[/] to start the generic host. The dashboard shows each hosted service, "
                + "its state and the log; Ctrl+C stops it.\n\nPublish one native executable with "
                + "[accent]dotnet publish -c Release -r win-x64[/] (or linux-x64, osx-arm64)."), "Service");
        });
        menu.Exit();
        menu.Run();
        return 0;
    }
}

/// <summary>The status report: a table and a summary line.</summary>
public static class Report {
    public static IWidget Build() {
        Table table = new Table();
        table.Column("Service");
        table.Column("State");
        table.Column("Uptime", HorizontalAlignment.Right);
        table.Column("Requests", HorizontalAlignment.Right);
        table.Row("api", TableCell.ForStatus(StatusKind.Pass, "healthy"), "3d 4h", "1 204 311");
        table.Row("worker", TableCell.ForStatus(StatusKind.Warn, "slow"), "3d 4h", "88 412");
        table.Row("reports", TableCell.ForStatus(StatusKind.Fail, "stopped"), "0s", "0");
        return new Rows(
            TextBlock.FromMarkup("[bold]Services[/] [muted]· sample data[/]"),
            table,
            TextBlock.FromMarkup("[pass]2 running[/] · [fail]1 stopped[/]"));
    }
}

/// <summary>The generic host: logging through ConsoleMonster, one worker, the live console dashboard.</summary>
public static class Service {
    public static async Task RunAsync(string[] args) {
        HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
        builder.Logging.ClearProviders();
        builder.Logging.AddConsoleMonster();
        builder.Services.AddHostedService<Worker>();
        // After AddHostedService: the dashboard lists the services registered before it.
        builder.Services.AddConsoleDashboard(dashboard => dashboard.Title = "ConsoleMonsterApp");
        await builder.Build().RunAsync().ConfigureAwait(false);
    }
}

/// <summary>A background job that reports its progress to the dashboard and the log.</summary>
public sealed class Worker : BackgroundService {
    private readonly ILogger<Worker> _log;
    private readonly IConsoleDashboard _dashboard;

    public Worker(ILogger<Worker> log, IConsoleDashboard dashboard) {
        _log = log;
        _dashboard = dashboard;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        int batch = 0;
        while (!stoppingToken.IsCancellationRequested) {
            batch++;
            _dashboard.SetDetail("Worker", "batch " + batch);
            _log.LogInformation("Processed batch {Batch}", batch);
            try {
                await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken).ConfigureAwait(false);
            } catch (OperationCanceledException) {
                break;
            }
        }
    }
}
C# fluentProgram.cs
namespace ConsoleMonsterApp;
/// <summary>
/// `dotnet run` opens the menu (or prints the report when the output is redirected);
/// `dotnet run -- serve` runs the generic host with the live console dashboard until Ctrl+C.
/// </summary>
public static class Program
{
    public static async Task<int> Main(string[] args)
    {
        if (args.Length > 0 && args[0] == "serve")
        {
            await Service.RunAsync(args).ConfigureAwait(false);
            return 0;
        }

        if (Terminal.Profile.Mode != OutputMode.Interactive)
        {
            // Redirected, CI or a pipe: the report as text, no menu.
            Report.Build().Show();
            return 0;
        }

        Menu menu = Ui.Menu()
            .Title("ConsoleMonsterApp")
            .Description("Pick an item with the arrow keys and Enter; Esc goes back.")
            .Item("Status report", item =>
            {
                item.Description("A table of services and a summary").PageBy(_ => Report.Build());
            })
            .Item("Run the service", item =>
            {
                item.Description("How to start the host with its live dashboard")
                    .PageBy(_ => Ui.Panel().Title("Service").Content(panelContent =>
                    {
                        panelContent.Markup(
                            ("Run [accent]dotnet run -- serve[/] to start the generic "
                                + "host. The dashboard shows each hosted service, ")
                                + ("its state and the log; Ctrl+C stops it.\n\nPublish one "
                                    + "native executable with ")
                                + ("[accent]dotnet publish -c Release -r win-x64[/] (or "
                                    + "linux-x64, osx-arm64)."));
                    }));
            })
            .Exit();
        menu.Run();
        return 0;
    }
}

/// <summary>The status report: a table and a summary line.</summary>
public static class Report
{
    public static IWidget Build()
    {
        Table table = Ui.Table()
            .Column("Service")
            .Column("State")
            .Column("Uptime", HorizontalAlignment.Right)
            .Column("Requests", HorizontalAlignment.Right)
            .Row("api", TableCell.ForStatus(StatusKind.Pass, "healthy"), "3d 4h", "1 204 311")
            .Row("worker", TableCell.ForStatus(StatusKind.Warn, "slow"), "3d 4h", "88 412")
            .Row("reports", TableCell.ForStatus(StatusKind.Fail, "stopped"), "0s", "0");
        return Ui.Rows().Content(rowsContent =>
        {
            rowsContent.Markup("[bold]Services[/] [muted]· sample data[/]");
            rowsContent.Add(table);
            rowsContent.Markup("[pass]2 running[/] · [fail]1 stopped[/]");
        });
    }
}

/// <summary>The generic host: logging through ConsoleMonster, one worker, the live console dashboard.</summary>
public static class Service
{
    public static async Task RunAsync(string[] args)
    {
        HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
        builder.Logging.ClearProviders();
        builder.Logging.AddConsoleMonster();
        builder.Services.AddHostedService<Worker>();

        // After AddHostedService: the dashboard lists the services registered before it.
        builder.Services.AddConsoleDashboard(dashboard => dashboard.Title("ConsoleMonsterApp"));
        await builder.Build().RunAsync().ConfigureAwait(false);
    }
}

/// <summary>A background job that reports its progress to the dashboard and the log.</summary>
public sealed class Worker : BackgroundService
{
    private readonly ILogger<Worker> _log;
    private readonly IConsoleDashboard _dashboard;
    public Worker(ILogger<Worker> log, IConsoleDashboard dashboard)
    {
        _log = log;
        _dashboard = dashboard;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        int batch = 0;
        while (!stoppingToken.IsCancellationRequested)
        {
            batch++;
            _dashboard.SetDetail("Worker", "batch " + batch);
            _log.LogInformation("Processed batch {Batch}", batch);
            try
            {
                await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                break;
            }
        }
    }
}
Full program
using ConsoleMonster;
using ConsoleMonster.Capabilities;
using ConsoleMonster.Extensions.Logging;
using ConsoleMonster.Hosting;
using ConsoleMonster.Interactive;
using ConsoleMonster.Layout;
using ConsoleMonster.Styling;
using ConsoleMonster.Widgets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ConsoleMonsterApp;
/// <summary>
/// `dotnet run` opens the menu (or prints the report when the output is redirected);
/// `dotnet run -- serve` runs the generic host with the live console dashboard until Ctrl+C.
/// </summary>
public static class Program
{
    public static async Task<int> Main(string[] args)
    {
        if (args.Length > 0 && args[0] == "serve")
        {
            await Service.RunAsync(args).ConfigureAwait(false);
            return 0;
        }

        if (Terminal.Profile.Mode != OutputMode.Interactive)
        {
            // Redirected, CI or a pipe: the report as text, no menu.
            Report.Build().Show();
            return 0;
        }

        Menu menu = Ui.Menu()
            .Title("ConsoleMonsterApp")
            .Description("Pick an item with the arrow keys and Enter; Esc goes back.")
            .Item("Status report", item =>
            {
                item.Description("A table of services and a summary").PageBy(_ => Report.Build());
            })
            .Item("Run the service", item =>
            {
                item.Description("How to start the host with its live dashboard")
                    .PageBy(_ => Ui.Panel().Title("Service").Content(panelContent =>
                    {
                        panelContent.Markup(
                            ("Run [accent]dotnet run -- serve[/] to start the generic "
                                + "host. The dashboard shows each hosted service, ")
                                + ("its state and the log; Ctrl+C stops it.\n\nPublish one "
                                    + "native executable with ")
                                + ("[accent]dotnet publish -c Release -r win-x64[/] (or "
                                    + "linux-x64, osx-arm64)."));
                    }));
            })
            .Exit();
        menu.Run();
        return 0;
    }
}

/// <summary>The status report: a table and a summary line.</summary>
public static class Report
{
    public static IWidget Build()
    {
        Table table = Ui.Table()
            .Column("Service")
            .Column("State")
            .Column("Uptime", HorizontalAlignment.Right)
            .Column("Requests", HorizontalAlignment.Right)
            .Row("api", TableCell.ForStatus(StatusKind.Pass, "healthy"), "3d 4h", "1 204 311")
            .Row("worker", TableCell.ForStatus(StatusKind.Warn, "slow"), "3d 4h", "88 412")
            .Row("reports", TableCell.ForStatus(StatusKind.Fail, "stopped"), "0s", "0");
        return Ui.Rows().Content(rowsContent =>
        {
            rowsContent.Markup("[bold]Services[/] [muted]· sample data[/]");
            rowsContent.Add(table);
            rowsContent.Markup("[pass]2 running[/] · [fail]1 stopped[/]");
        });
    }
}

/// <summary>The generic host: logging through ConsoleMonster, one worker, the live console dashboard.</summary>
public static class Service
{
    public static async Task RunAsync(string[] args)
    {
        HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
        builder.Logging.ClearProviders();
        builder.Logging.AddConsoleMonster();
        builder.Services.AddHostedService<Worker>();

        // After AddHostedService: the dashboard lists the services registered before it.
        builder.Services.AddConsoleDashboard(dashboard => dashboard.Title("ConsoleMonsterApp"));
        await builder.Build().RunAsync().ConfigureAwait(false);
    }
}

/// <summary>A background job that reports its progress to the dashboard and the log.</summary>
public sealed class Worker : BackgroundService
{
    private readonly ILogger<Worker> _log;
    private readonly IConsoleDashboard _dashboard;
    public Worker(ILogger<Worker> log, IConsoleDashboard dashboard)
    {
        _log = log;
        _dashboard = dashboard;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        int batch = 0;
        while (!stoppingToken.IsCancellationRequested)
        {
            batch++;
            _dashboard.SetDetail("Worker", "batch " + batch);
            _log.LogInformation("Processed batch {Batch}", batch);
            try
            {
                await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                break;
            }
        }
    }
}

The recording runs the template's own Program.cs, built against the ConsoleMonster projects of the same source as this site, with the keys pressed for it: the menu opens the status report and the service page, Exit leaves it, and dotnet run -- serve starts the host with its dashboard until Ctrl+C. When the output is redirected, the app prints the report instead of opening the menu.

OptionDefaultMeaning
--Frameworknet8.0net8.0 or net10.0.
--ConsoleMonsterVersion3.2.0The ConsoleMonster package version; the template follows each release.
--NoAotfalseLeaves native AOT publishing off.

The app uses a Menu with pages, a Table with status cells, logging through ConsoleMonster.Extensions.Logging and the hosting dashboard.

For what else a menu can open, see menus, forms and apps.

Next

Edit this page on GitHub