CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

Cannot adjust logging verbosity through HostBuilder

Open wasabii opened this issue 5 years ago • 3 comments
trafficstars

Oversight. It is a common pattern to add an Option like -d or -v to the command line application in order to adjust logging level. However, since logging is configured up in the HostBuilder, before invoking RunCommandLineApplicationAsync, there is no way to obtain this parsed data in order to decide on the MinimumLogLevel set in AddConsole (or whatever).

Not sure on a solution to this. Chicken and egg problem.

wasabii avatar Mar 17 '20 19:03 wasabii

Thinking through solutions. It might make sense to not use the standard .NET Core Console Logger provider for this. But instead to provide a custom one that filters log level and allows adjustment of log level through a singleton instance provided to the Command classes. Might also make sense to not circumvent CommandLineUtils IConsole instance either, and run logging through that.

            return await new HostBuilder()
                .ConfigureLogging(o => /* no longer required */)
                .ConfigureServices((ctx, svc) => svc.AddSingleton(PhysicalConsole.Singleton))
                .RunCommandLineApplicationAsync<Program>(args);

Maybe RunCommandLineApplicationAsync could append additional logger outputs. One of this sends through IConsole. This eliminating the need for AddConsole. And also provide something like a IConsoleLoggerConfiguration that could be injected into a class, with a MinimumLevel property.

wasabii avatar Mar 17 '20 19:03 wasabii

I'm open to suggestions on how to implement this one. Adding the help-wanted label. I think you're right...it is sort of a chicken and egg problem in the code's current format.

natemcmaster avatar Mar 27 '20 14:03 natemcmaster

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

stale[bot] avatar Jul 21 '21 02:07 stale[bot]

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

github-actions[bot] avatar Nov 14 '22 02:11 github-actions[bot]

Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.

github-actions[bot] avatar Nov 29 '22 02:11 github-actions[bot]