CommandLineUtils
CommandLineUtils copied to clipboard
Cannot adjust logging verbosity through HostBuilder
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.
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.
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.
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.
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.
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.