From eaad67dad8474e520cd6acf5237b7809c117a5da Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 6 Aug 2017 18:37:49 -0500 Subject: [PATCH] (GH-1378) allow external log4net config file If choco finds a log file next to it by the name of `log4net.config.xml` (a little bit different on purpose), it will use that file instead of the built-in configuration for log4net. Use this with caution as it could cause certain logging items to disappear when the external config does not include the necessary logging sections. It may even cause choco to error. --- .../logging/Log4NetAppenderConfiguration.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs index 5997eb24bd..a5143fc30d 100644 --- a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs +++ b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs @@ -47,24 +47,33 @@ public sealed class Log4NetAppenderConfiguration public static void configure(string outputDirectory = null, params string[] excludeLoggerNames) { GlobalContext.Properties["pid"] = System.Diagnostics.Process.GetCurrentProcess().Id; - - var assembly = Assembly.GetExecutingAssembly(); - var resource = ApplicationParameters.Log4NetConfigurationResource; - if (Platform.get_platform() != PlatformType.Windows) + + var xmlConfigFile = Path.Combine(ApplicationParameters.InstallLocation, "log4net.config.xml"); + if (File.Exists(xmlConfigFile)) { - // it became much easier to do this once we realized that updating the current mappings is about impossible. - resource = resource.Replace("log4net.", "log4net.mono."); + XmlConfigurator.ConfigureAndWatch(new FileInfo(xmlConfigFile)); + _logger.DebugFormat("Configured Log4Net configuration from file ('{0}').", xmlConfigFile); } - Stream xmlConfigStream = assembly.get_manifest_stream(resource); + else + { + var assembly = Assembly.GetExecutingAssembly(); + var resource = ApplicationParameters.Log4NetConfigurationResource; + if (Platform.get_platform() != PlatformType.Windows) + { + // it became much easier to do this once we realized that updating the current mappings is about impossible. + resource = resource.Replace("log4net.", "log4net.mono."); + } + Stream xmlConfigStream = assembly.get_manifest_stream(resource); - XmlConfigurator.Configure(xmlConfigStream); + XmlConfigurator.Configure(xmlConfigStream); + _logger.DebugFormat("Configured Log4Net configuration ('{0}') from assembly {1}", resource, assembly.FullName); + } + if (!string.IsNullOrWhiteSpace(outputDirectory)) { set_file_appender(outputDirectory, excludeLoggerNames); } - - _logger.DebugFormat("Configured {0} from assembly {1}", resource, assembly.FullName); } ///