diff --git a/dev-proxy/Program.cs b/dev-proxy/Program.cs index 020791d3..f1cfc101 100644 --- a/dev-proxy/Program.cs +++ b/dev-proxy/Program.cs @@ -7,6 +7,11 @@ PluginEvents pluginEvents = new PluginEvents(); ILogger logger = new ConsoleLogger(ProxyCommandHandler.Configuration, pluginEvents); +// set the log level if specified through args +if (ProxyHost.LogLevel is not null) +{ + logger.LogLevel = ProxyHost.LogLevel.Value; +} IProxyContext context = new ProxyContext(logger, ProxyCommandHandler.Configuration); ProxyHost proxyHost = new(); RootCommand rootCommand = proxyHost.GetRootCommand(logger); diff --git a/dev-proxy/ProxyHost.cs b/dev-proxy/ProxyHost.cs index 17d505bf..c1d29507 100644 --- a/dev-proxy/ProxyHost.cs +++ b/dev-proxy/ProxyHost.cs @@ -11,7 +11,7 @@ internal class ProxyHost { private Option _portOption; private Option _ipAddressOption; - private Option _logLevelOption; + private static Option? _logLevelOption; private Option _recordOption; private Option?> _watchPidsOption; private Option?> _watchProcessNamesOption; @@ -87,8 +87,56 @@ public static string ConfigFile } } - public ProxyHost() + private static bool _logLevelResolved = false; + private static LogLevel? _logLevel; + public static LogLevel? LogLevel { + get + { + if (_logLevelResolved) + { + return _logLevel; + } + + if (_logLevelOption is null) + { + _logLevelOption = new Option( + "--log-level", + $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}" + ) + { + ArgumentHelpName = "logLevel" + }; + _logLevelOption.AddValidator(input => { + if (!Enum.TryParse(input.Tokens.First().Value, true, out var logLevel)) { + input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}"; + } + }); + } + + var result = _logLevelOption.Parse(Environment.GetCommandLineArgs()); + // since we're parsing all args, and other options are not instantiated yet + // we're getting here a bunch of other errors, so we only need to look for + // errors related to the log level option + var error = result.Errors.Where(e => e.SymbolResult?.Symbol == _logLevelOption).FirstOrDefault(); + if (error is not null) + { + // Logger is not available here yet so we need to fallback to Console + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; + Console.Error.WriteLine(error.Message); + Console.ForegroundColor = color; + Environment.Exit(1); + } + + _logLevel = result.GetValueForOption(_logLevelOption); + _logLevelResolved = true; + + return _logLevel; + } + } + + public ProxyHost() { _portOption = new Option("--port", "The port for the proxy to listen on"); _portOption.AddAlias("-p"); _portOption.ArgumentHelpName = "port"; @@ -105,16 +153,6 @@ public ProxyHost() } }); - _logLevelOption = new Option("--log-level", $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}"); - _logLevelOption.ArgumentHelpName = "logLevel"; - _logLevelOption.AddValidator(input => - { - if (!Enum.TryParse(input.Tokens.First().Value, true, out var logLevel)) - { - input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}"; - } - }); - _recordOption = new Option("--record", "Use this option to record all request logs"); _watchPidsOption = new Option?>("--watch-pids", "The IDs of processes to watch for requests"); @@ -145,7 +183,9 @@ public RootCommand GetRootCommand(ILogger logger) var command = new RootCommand { _portOption, _ipAddressOption, - _logLevelOption, + // _logLevelOption is set while initialize the Program + // As such, it's always set here + _logLevelOption!, _recordOption, _watchPidsOption, _watchProcessNamesOption,