Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.48k stars 60 forks source link

Capturing Trace and Debug output #131

Closed thegreatco closed 2 years ago

thegreatco commented 2 years ago

Details

It would be helpful if we could somehow capture the output written with System.Diagnostics.Trace and System.Diagnostics.Debug and have that get written to IConsole.

Tyrrrz commented 2 years ago

It would be something like this:

public async ValueTask ExecuteAsync(IConsole console)
{
    var traceListener = new TextWriterTraceListener(console.Output);
    Trace.Listeners.Add(traceListener);

    // ...
    Trace.Listeners.Remove(traceListener);
}

Or at the application level:

public static async Task<int> Main()
{
    var console = new SystemConsole();

    var traceListener = new TextWriterTraceListener(console.Output);
    Trace.Listeners.Add(traceListener);

    return await new CliApplicationBuilder()
        .AddCommandsFromThisAssembly()
        .UseConsole(console)
        .Build()
        .RunAsync();
}