From c6cc5b61820dee5b99501c3f74328ee526410b2f Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 16 Dec 2018 15:37:33 -0600 Subject: [PATCH] (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 @@ -