From 9e7d42f978680db6e91653fdb7535ccc215602a2 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 6 Dec 2018 11:14:15 -0600 Subject: [PATCH 1/7] (GH-1292) add note on manifest caching Windows behavior of manifest caching is enough to make even the best of admins and developers want to tear out their hair until they realize there is a cache and changes to the manifest are completely ignored by Windows. So leave a breadcrumb here so that folks know if they make a change to the manifest file, they will need to "touch" the file they are trying to get the manifest to take effect for - in this case choco.exe. The modification date will need changed to clue Windows in that there is a new manifest. This is only when changing the file AFTER the exe / modification date has been run even once. It's better to just say to always update the modification date as someone may not know whether the file has been ran before. --- src/chocolatey.console/choco.exe.manifest | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/chocolatey.console/choco.exe.manifest b/src/chocolatey.console/choco.exe.manifest index ed065cf93d..6abc70c0be 100644 --- a/src/chocolatey.console/choco.exe.manifest +++ b/src/chocolatey.console/choco.exe.manifest @@ -27,6 +27,13 @@ The following is an unsupported use case: If you want to utilize File and Registry Virtualization for backward compatibility then delete the requestedExecutionLevel node. + + NOTE: If you do change this file, make sure that you change the + modification date on choco.exe that this sits next to. Windows + caches manifests based on path and last modified date. So you'll + need to change the modification date on choco.exe for the manifest + change to take effect. + Details: https://github.com/chocolatey/choco/issues/1292#issuecomment-304068121 --> From a87aeb70f222e61df8682d3a0577e836d31ff544 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 14:32:18 -0600 Subject: [PATCH 2/7] (GH-1503) Warn on incorrectly named license file If choco does not find a valid license, but it finds other files in the license folder, assume that a user misnamed the file or did not rename it properly. Log an error and warning with information on how to correct the issue. If a user puts the license in the top level folder, and also if they forget to rename the file, log an error and warning with information on how to correct the issue. --- .../licensing/LicenseValidation.cs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure/licensing/LicenseValidation.cs b/src/chocolatey/infrastructure/licensing/LicenseValidation.cs index 7152abf12e..b866369864 100644 --- a/src/chocolatey/infrastructure/licensing/LicenseValidation.cs +++ b/src/chocolatey/infrastructure/licensing/LicenseValidation.cs @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.licensing using System; using System.IO; using app; + using logging; using Rhino.Licensing; public sealed class LicenseValidation @@ -37,7 +38,35 @@ public static ChocolateyLicense validate() var userLicenseFile = ApplicationParameters.UserLicenseFileLocation; if (File.Exists(userLicenseFile)) licenseFile = userLicenseFile; - //no IFileSystem at this point + // no IFileSystem at this point + if (!File.Exists(licenseFile)) + { + var licenseFileName = Path.GetFileName(ApplicationParameters.LicenseFileLocation); + var licenseDirectory = Path.GetDirectoryName(ApplicationParameters.LicenseFileLocation); + + // look for misnamed files and locations + // - look in the license directory for misnamed files + if (Directory.Exists(licenseDirectory)) + { + if (Directory.GetFiles(licenseDirectory).Length != 0) + { + "chocolatey".Log().Error(@"Files found in directory '{0}' but not a + valid license file. License should be named '{1}'.".format_with(licenseDirectory, licenseFileName)); + "chocolatey".Log().Warn(ChocolateyLoggers.Important,@" Rename license file to '{0}' to allow commercial features.".format_with(licenseFileName)); + } + } + + + // - user put the license file in the top level location and/or forgot to rename it + if (File.Exists(Path.Combine(ApplicationParameters.InstallLocation, licenseFileName)) || File.Exists(Path.Combine(ApplicationParameters.InstallLocation, licenseFileName + ".txt"))) + { + "chocolatey".Log().Error(@"Chocolatey license found in the wrong location. File must be located at + '{0}'.".format_with(ApplicationParameters.LicenseFileLocation)); + "chocolatey".Log().Warn(ChocolateyLoggers.Important, @" Move license file to '{0}' to allow commercial features.".format_with(ApplicationParameters.LicenseFileLocation)); + } + } + + // no IFileSystem at this point if (File.Exists(licenseFile)) { "chocolatey".Log().Debug("Evaluating license file found at '{0}'".format_with(licenseFile)); From a3fec0c98a6e8e35a04511a36036a18b6151fa88 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 14:47:25 -0600 Subject: [PATCH 3/7] (doc) More info on license next steps Split the error to an error with a warning on actionable next steps. Mention that with a trial they have additional work to do to give a user information on next steps. --- src/chocolatey/infrastructure/licensing/License.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/chocolatey/infrastructure/licensing/License.cs b/src/chocolatey/infrastructure/licensing/License.cs index eca8009199..73469127d1 100644 --- a/src/chocolatey/infrastructure/licensing/License.cs +++ b/src/chocolatey/infrastructure/licensing/License.cs @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.licensing using adapters; using app; using information; + using logging; using registration; using Environment = System.Environment; @@ -45,12 +46,19 @@ public static ChocolateyLicense validate_license() @"Error when attempting to load chocolatey licensed assembly. Ensure that chocolatey.licensed.dll exists at '{0}'. - Install with `choco install chocolatey.extension`. - The error message itself may be helpful as well:{1} {2}".format_with( + The error message itself may be helpful:{1} {2}".format_with( ApplicationParameters.LicensedAssemblyLocation, Environment.NewLine, ex.Message )); + "chocolatey".Log().Warn(ChocolateyLoggers.Important,@" Install the Chocolatey Licensed Extension package with + `choco install chocolatey.extension` to remove this license warning. + TRIALS: If you have a trial license, you cannot use the above command + as is and be successful. You need to download nupkgs from the links in + the trial email as your license will not be registered on the licensed + repository. Please reference + https://chocolatey.org/docs/installation-licensed#how-do-i-install-the-trial-edition + for specific instructions."); } } From f0df5c0296bf3519b2dc2fc69b859dafb79e2437 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 14:52:27 -0600 Subject: [PATCH 4/7] (GH-1665) sign init.ps1 file Stop excluding the init.ps1 file from being authenticode signed as it might be pulled into something and cause blocks. Even though it is not used, it should not trigger alerts for being unsigned. --- .build.custom/codeSign.step | 1 - 1 file changed, 1 deletion(-) diff --git a/.build.custom/codeSign.step b/.build.custom/codeSign.step index ebbc20ddca..0296ee88ba 100644 --- a/.build.custom/codeSign.step +++ b/.build.custom/codeSign.step @@ -121,7 +121,6 @@ Set-AuthenticodeSignature -Filepath @("C:\ProgramData\chocolatey\helpers\chocola - From c6cc5b61820dee5b99501c3f74328ee526410b2f Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 15:37:33 -0600 Subject: [PATCH 5/7] (GH-1131) logging: better defaults for INFO For INFO important messages that use Green, do not override the background color unless that color conflicts with visibility. For regular INFO logs, use the same colors already configured in the console. --- .../logging/Log4NetAppenderConfiguration.cs | 79 +++++++++++++++++++ .../infrastructure/logging/log4net.config.xml | 1 - 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs index 5bc66be94b..e7ecae4da7 100644 --- a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs +++ b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs @@ -16,7 +16,9 @@ namespace chocolatey.infrastructure.logging { + using System; using System.Collections.Generic; + using System.ComponentModel; using System.IO; using System.Linq; using adapters; @@ -30,13 +32,27 @@ namespace chocolatey.infrastructure.logging using log4net.Repository; using log4net.Repository.Hierarchy; using platforms; + using Console = adapters.Console; public sealed class Log4NetAppenderConfiguration { private static readonly log4net.ILog _logger = LogManager.GetLogger(typeof(Log4NetAppenderConfiguration)); + private static Lazy _console = new Lazy(() => new Console()); + [EditorBrowsable(EditorBrowsableState.Never)] + public static void initialize_with(Lazy console) + { + _console = console; + } + + private static IConsole Console + { + get { return _console.Value; } + } private static bool _alreadyConfiguredFileAppender; private static readonly string _summaryLogAppenderName = "{0}.summary.log.appender".format_with(ApplicationParameters.Name); + private const string NORMAL_LOGGING_COLORED_APPENDER = "NormalLoggingColoredConsoleAppender"; + private const string IMPORTANT_LOGGING_COLORED_APPENDER = "ImportantLoggingColoredConsoleAppender"; /// /// Pulls xml configuration from embedded location and applies it. @@ -69,6 +85,8 @@ public static void configure(string outputDirectory = null, params string[] excl _logger.DebugFormat("Configured Log4Net configuration ('{0}') from assembly {1}", resource, assembly.FullName); } + + configure_info_logging_colors(); if (!string.IsNullOrWhiteSpace(outputDirectory)) { @@ -76,6 +94,67 @@ public static void configure(string outputDirectory = null, params string[] excl } } + private static void configure_info_logging_colors() + { + try + { + // configure INFO on same as current background color and foreground colors + var bgColor = Console.BackgroundColor; + var fgColor = Console.ForegroundColor; + ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetCallingAssembly().UnderlyingType); + foreach (var append in logRepository.GetAppenders().Where(a => a.Name.is_equal_to(NORMAL_LOGGING_COLORED_APPENDER)).or_empty_list_if_null()) + { + var appender = append as ManagedColoredConsoleAppender; + if (appender != null) + { + var infoMapping = new ManagedColoredConsoleAppender.LevelColors + { + Level = Level.Info, + BackColor = bgColor, + ForeColor = fgColor, + }; + appender.AddMapping(infoMapping); + appender.ActivateOptions(); + } + } + + foreach (var append in logRepository.GetAppenders().Where(a => a.Name.is_equal_to(IMPORTANT_LOGGING_COLORED_APPENDER)).or_empty_list_if_null()) + { + var appender = append as ManagedColoredConsoleAppender; + if (appender != null) + { + // add black based on current background color + if (bgColor == ConsoleColor.White + || bgColor == ConsoleColor.Gray + || bgColor == ConsoleColor.Yellow + || bgColor == ConsoleColor.DarkYellow + || bgColor == ConsoleColor.DarkCyan + || bgColor == ConsoleColor.DarkGray + || bgColor == ConsoleColor.DarkGreen + || bgColor == ConsoleColor.Green + || bgColor == ConsoleColor.Cyan + || bgColor == ConsoleColor.Magenta + ) + { + var infoMapping = new ManagedColoredConsoleAppender.LevelColors + { + Level = Level.Info, + BackColor = ConsoleColor.Black, + ForeColor = ConsoleColor.Green, + }; + appender.AddMapping(infoMapping); + + appender.ActivateOptions(); + } + } + } + } + catch (Exception) + { + // ignore this and move on + } + } + /// /// Adds a file appender to all current loggers. Only runs one time. /// diff --git a/src/chocolatey/infrastructure/logging/log4net.config.xml b/src/chocolatey/infrastructure/logging/log4net.config.xml index 758e1cb1e1..54392bf313 100644 --- a/src/chocolatey/infrastructure/logging/log4net.config.xml +++ b/src/chocolatey/infrastructure/logging/log4net.config.xml @@ -57,7 +57,6 @@ - From 38763a13b0789143c2fdc6bccd0b4d22f61cd1f5 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 16:18:33 -0600 Subject: [PATCH 6/7] (GH-1131) logging: better defaults for WARN For warning messages, remove the background color unless that color conflicts with visibility of the text. --- .../logging/Log4NetAppenderConfiguration.cs | 25 +++++++++++++++++++ .../infrastructure/logging/log4net.config.xml | 2 -- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs index e7ecae4da7..50f3a7ecb9 100644 --- a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs +++ b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs @@ -114,6 +114,23 @@ private static void configure_info_logging_colors() ForeColor = fgColor, }; appender.AddMapping(infoMapping); + + // make sure warnings can be clearly seen + if (bgColor == ConsoleColor.White + || bgColor == ConsoleColor.Gray + || bgColor == ConsoleColor.Yellow + || bgColor == ConsoleColor.DarkYellow + || bgColor == ConsoleColor.DarkCyan + ) + { + var warnMapping = new ManagedColoredConsoleAppender.LevelColors + { + Level = Level.Warn, + BackColor = ConsoleColor.Black, + ForeColor = ConsoleColor.Yellow, + }; + appender.AddMapping(warnMapping); + } appender.ActivateOptions(); } } @@ -144,6 +161,14 @@ private static void configure_info_logging_colors() }; appender.AddMapping(infoMapping); + var warnMapping = new ManagedColoredConsoleAppender.LevelColors + { + Level = Level.Warn, + BackColor = ConsoleColor.Black, + ForeColor = ConsoleColor.Magenta, + }; + appender.AddMapping(warnMapping); + appender.ActivateOptions(); } } diff --git a/src/chocolatey/infrastructure/logging/log4net.config.xml b/src/chocolatey/infrastructure/logging/log4net.config.xml index 54392bf313..939947406c 100644 --- a/src/chocolatey/infrastructure/logging/log4net.config.xml +++ b/src/chocolatey/infrastructure/logging/log4net.config.xml @@ -17,7 +17,6 @@ - @@ -52,7 +51,6 @@ - From b0f1424036fce1df89b08489947db60b56f86024 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 16:19:29 -0600 Subject: [PATCH 7/7] (GH-1131) logging: better defaults for ERROR For error messages, remove the background color unless that color conflicts with the visibility of the text. --- .../logging/Log4NetAppenderConfiguration.cs | 21 +++++++++++++++++++ .../infrastructure/logging/log4net.config.xml | 1 - 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs index 50f3a7ecb9..339fc73c89 100644 --- a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs +++ b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs @@ -131,6 +131,27 @@ private static void configure_info_logging_colors() }; appender.AddMapping(warnMapping); } + + // make sure errors can be clearly seen + if (bgColor == ConsoleColor.Red + || bgColor == ConsoleColor.DarkRed + || bgColor == ConsoleColor.Yellow + || bgColor == ConsoleColor.DarkYellow + || bgColor == ConsoleColor.DarkCyan + || bgColor == ConsoleColor.DarkGray + || bgColor == ConsoleColor.DarkGreen + || bgColor == ConsoleColor.Blue + ) + { + var errorMapping = new ManagedColoredConsoleAppender.LevelColors + { + Level = Level.Error, + BackColor = ConsoleColor.Black, + ForeColor = ConsoleColor.Red, + }; + appender.AddMapping(errorMapping); + } + appender.ActivateOptions(); } } diff --git a/src/chocolatey/infrastructure/logging/log4net.config.xml b/src/chocolatey/infrastructure/logging/log4net.config.xml index 939947406c..9bc45d7be4 100644 --- a/src/chocolatey/infrastructure/logging/log4net.config.xml +++ b/src/chocolatey/infrastructure/logging/log4net.config.xml @@ -12,7 +12,6 @@ -