From ff48645d92851cf71198ca6f8da1337d0e995cea Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 4 Feb 2016 10:36:17 -0600 Subject: [PATCH 1/3] (GH-8) Always set Debug/Verbose for host Our internal host uses our logging techniques, so we always want to capture both Debug and Verbose output from PowerShell. --- .../infrastructure.app/services/PowershellService.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index e211d38b78..507e814be0 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -407,11 +407,15 @@ private void remove_assembly_resolver() private PowerShellExecutionResults run_host(ChocolateyConfiguration config, string chocoPowerShellScript) { + // since we control output in the host, always set these true + Environment.SetEnvironmentVariable("ChocolateyEnvironmentDebug", "true"); + Environment.SetEnvironmentVariable("ChocolateyEnvironmentVerbose", "true"); + var result = new PowerShellExecutionResults(); string commandToRun = wrap_script_with_module(chocoPowerShellScript, config); var host = new PoshHost(config); this.Log().Debug(() => "Calling built-in PowerShell host with ['{0}']".format_with(commandToRun.escape_curly_braces())); - + var initialSessionState = InitialSessionState.CreateDefault(); // override system execution policy without accidentally setting it initialSessionState.AuthorizationManager = new AuthorizationManager("choco"); From 08909a67c16c39edd1593128946a7376b079dc64 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 4 Feb 2016 10:40:55 -0600 Subject: [PATCH 2/3] (GH-611) Fix - Verbose shows up on Debug Verbose output should only be shown in output when a user uses the verbose switch. The naive implementation of the debug filter was clearing the filter levels for Verbose. Exclude the verbose logger from the filter clear. --- src/chocolatey.console/Program.cs | 5 +++-- .../runners/ConsoleApplication.cs | 2 +- .../logging/Log4NetAppenderConfiguration.cs | 15 ++++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/chocolatey.console/Program.cs b/src/chocolatey.console/Program.cs index 85942e8ad9..4959d58144 100644 --- a/src/chocolatey.console/Program.cs +++ b/src/chocolatey.console/Program.cs @@ -17,6 +17,7 @@ namespace chocolatey.console { using System; using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using infrastructure.adapters; @@ -131,8 +132,8 @@ that chocolatey.licensed.dll exists at Environment.Exit(-1); } - Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(config.Verbose, "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); - Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug); + Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, excludeLoggerName: "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); + Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(config.Verbose, config.Debug, "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); "chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".format_with(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.to_string())); //"chocolatey".Log().Debug(() => "Command Line: {0}".format_with(Environment.CommandLine)); diff --git a/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs b/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs index 2084fe639c..39e9185c97 100644 --- a/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs +++ b/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs @@ -56,7 +56,7 @@ public void run(string[] args, ChocolateyConfiguration config, Container contain // if debug is bundled with local options, it may not get picked up when global // options are parsed. Attempt to set it again once local options are set. // This does mean some output from debug will be missed (but not much) - if (config.Debug) Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug); + if (config.Debug) Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, excludeLoggerName: "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); command.handle_additional_argument_parsing(unparsedArgs, config); diff --git a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs index ee2ab5dcb6..0f5e85a636 100644 --- a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs +++ b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs @@ -16,6 +16,7 @@ namespace chocolatey.infrastructure.logging { using System.IO; + using System.Linq; using adapters; using app; using log4net; @@ -108,7 +109,8 @@ private static void set_file_appender(string outputDirectory) /// /// if set to true [enable debug]. /// - public static void set_logging_level_debug_when_debug(bool enableDebug) + /// Logger, such as a verbose logger, to exclude from this. + public static void set_logging_level_debug_when_debug(bool enableDebug, string excludeLoggerName) { if (enableDebug) { @@ -121,8 +123,9 @@ public static void set_logging_level_debug_when_debug(bool enableDebug) { logger.Level = Level.Debug; } - } - foreach (var append in logRepository.GetAppenders()) + } + + foreach (var append in logRepository.GetAppenders().Where(a => !a.Name.is_equal_to(excludeLoggerName.to_string())).or_empty_list_if_null()) { var appender = append as AppenderSkeleton; if (appender != null) @@ -140,8 +143,9 @@ public static void set_logging_level_debug_when_debug(bool enableDebug) /// /// if set to true [enable verbose]. /// + /// If debug is also enabled /// Name of the verbose logger. - public static void set_verbose_logger_when_verbose(bool enableVerbose, string verboseLoggerName) + public static void set_verbose_logger_when_verbose(bool enableVerbose,bool enableDebug, string verboseLoggerName) { if (enableVerbose) { @@ -153,7 +157,8 @@ public static void set_verbose_logger_when_verbose(bool enableVerbose, string ve if (appender != null && appender.Name.is_equal_to(verboseLoggerName)) { appender.ClearFilters(); - appender.AddFilter( new log4net.Filter.LevelRangeFilter {LevelMin = Level.Info, LevelMax = Level.Fatal}); + var minLevel = enableDebug ? Level.Debug : Level.Info; + appender.AddFilter(new log4net.Filter.LevelRangeFilter { LevelMin = minLevel, LevelMax = Level.Fatal }); } } From 6dffaff284dcd4e94fcd9e0854fc8592e08fa856 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 4 Feb 2016 10:46:10 -0600 Subject: [PATCH 3/3] (GH-603) Get-FileName should set proxy if required As the other two web calls check/use proxy and network credentials, Get-FileName should also use a proxy if required. --- .../helpers/functions/Get-FileName.ps1 | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/chocolatey.resources/helpers/functions/Get-FileName.ps1 b/src/chocolatey.resources/helpers/functions/Get-FileName.ps1 index 9ceb76e327..7fc3200000 100644 --- a/src/chocolatey.resources/helpers/functions/Get-FileName.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-FileName.ps1 @@ -31,6 +31,47 @@ param( return $originalFileName } + $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials + if ($defaultCreds -ne $null) { + $request.Credentials = $defaultCreds + } + + $client = New-Object System.Net.WebClient + if ($defaultCreds -ne $null) { + $client.Credentials = $defaultCreds + } + + # check if a proxy is required + $explicitProxy = $env:chocolateyProxyLocation + $explicitProxyUser = $env:chocolateyProxyUser + $explicitProxyPassword = $env:chocolateyProxyPassword + if ($explicitProxy -ne $null) { + # explicit proxy + $proxy = New-Object System.Net.WebProxy($explicitProxy, $true) + if ($explicitProxyPassword -ne $null) { + $passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText -Force + $proxy.Credentials = New-Object System.Management.Automation.PSCredential ($explicitProxyUser, $passwd) + } + + Write-Debug "Using explicit proxy server '$explicitProxy'." + $request.Proxy = $proxy + + } elseif (!$client.Proxy.IsBypassed($url)) + { + # system proxy (pass through) + $creds = [Net.CredentialCache]::DefaultCredentials + if ($creds -eq $null) { + Write-Debug "Default credentials were null. Attempting backup method" + $cred = Get-Credential + $creds = $cred.GetNetworkCredential(); + } + $proxyAddress = $client.Proxy.GetProxy($url).Authority + Write-Debug "Using system proxy server '$proxyaddress'." + $proxy = New-Object System.Net.WebProxy($proxyAddress) + $proxy.Credentials = $creds + $request.Proxy = $proxy + } + $request.Method = "GET" $request.Accept = '*/*' $request.AllowAutoRedirect = $true