diff --git a/XO.Console.Cli.Extensions/CommandAppHostBuilderExtensions.cs b/XO.Console.Cli.Extensions/CommandAppHostBuilderExtensions.cs index 3fd3b95..3eff339 100644 --- a/XO.Console.Cli.Extensions/CommandAppHostBuilderExtensions.cs +++ b/XO.Console.Cli.Extensions/CommandAppHostBuilderExtensions.cs @@ -73,18 +73,10 @@ internal static async Task RunCommandAppAsync( hostBuilder.ConfigureServices( (_, services) => services.AddCommandApp(builderFactory, configure)); - IHost host; - try - { - host = hostBuilder.Build(); - } - catch (Exception ex) - { - System.Console.Error.WriteLine(ex.Message); - return -1; - } + // build the host + using var host = hostBuilder.Build(); - // get a reference to the logger factory + // get a reference to the logger factory so we can dispose it last var loggerFactory = host.Services.GetService(); // run the host @@ -94,11 +86,6 @@ internal static async Task RunCommandAppAsync( result = await host.RunCommandAppAsync(args) .ConfigureAwait(false); } - catch (Exception ex) - { - System.Console.Error.WriteLine(ex.Message); - result = -1; - } finally { await DisposeAndFlush(host, loggerFactory) @@ -118,11 +105,6 @@ private static ValueTask DisposeAndFlush(IHost host, ILoggerFactory? loggerFacto host.Dispose(); return ValueTask.CompletedTask; } - catch (Exception ex) - { - System.Diagnostics.Debug.WriteLine(ex.ToString()); - return ValueTask.CompletedTask; - } finally { loggerFactory?.Dispose(); diff --git a/XO.Console.Cli.Extensions/CommandAppHostExtensions.cs b/XO.Console.Cli.Extensions/CommandAppHostExtensions.cs index 506cdc4..b6d467f 100644 --- a/XO.Console.Cli.Extensions/CommandAppHostExtensions.cs +++ b/XO.Console.Cli.Extensions/CommandAppHostExtensions.cs @@ -42,17 +42,14 @@ public static async Task RunCommandAppAsync(this IHost host, IReadOnlyList< try { logger?.LogDebug("Executing command {Command} ... ", from x in parse.GetVerbs() select x.Value); - logger?.LogTrace("Command args: {Args}", args); result = await app.ExecuteAsync(parse, lifetime.ApplicationStopping) .ConfigureAwait(false); } catch (Exception ex) { - logger?.LogCritical(ex, "Unhandled exception: {ExceptionMessage}", ex.Message); - - System.Console.Error.WriteLine(ex.Message); - result = 1; + logger?.LogCritical(ex, "An unhandled exception occurred while executing the command."); + throw; } finally {