From b0c89b17e29289fea057b982d1adbc4d79a5cc8a Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Mon, 21 Jun 2021 02:14:37 -0700 Subject: [PATCH] (#2124) Add ability to initialize without logging This commit makes changes to the API initialization method to allow Chocolatey to allow the initialization to happen without enabling logging. Additionally changes have been made to allow adding extra loggers to be added without overriding the logging to the applications main chocolatey.log file. These changes were necessary to allow programs (like ChocolateyGUI) to add its own logging infrastructure without disabling logging to the main log file. --- src/chocolatey/GetChocolatey.cs | 57 ++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index 65b80c5755..4a7a9db04a 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -37,6 +37,7 @@ namespace chocolatey using Assembly = infrastructure.adapters.Assembly; using IFileSystem = infrastructure.filesystem.IFileSystem; using ILog = infrastructure.logging.ILog; + using System.IO; // ReSharper disable InconsistentNaming @@ -47,16 +48,21 @@ public static class Lets { private static readonly log4net.ILog _logger = LogManager.GetLogger(typeof(Lets)); - private static GetChocolatey set_up() + private static GetChocolatey set_up(bool initializeLogging) { add_assembly_resolver(); - return new GetChocolatey(); + return new GetChocolatey(initializeLogging); } public static GetChocolatey GetChocolatey() { - return GlobalMutex.enter(() => set_up(), 10); + return GetChocolatey(true); + } + + public static GetChocolatey GetChocolatey(bool initializeLogging) + { + return GlobalMutex.enter(() => set_up(initializeLogging), 10); } private static ResolveEventHandler _handler = null; @@ -122,10 +128,25 @@ public class GetChocolatey /// Initializes a new instance of the class. /// public GetChocolatey() + : this(true) + { + } + + public GetChocolatey(bool initializeLogging) { - Log4NetAppenderConfiguration.configure(null, excludeLoggerNames: ChocolateyLoggers.Trace.to_string()); - Bootstrap.initialize(); - Log.InitializeWith(new AggregateLog(new List() { new Log4NetLog(), _logSinkLogger })); + if (initializeLogging) + { + string loggingLocation = ApplicationParameters.LoggingLocation; + + if (!Directory.Exists(loggingLocation)) + { + Directory.CreateDirectory(loggingLocation); + } + + Log4NetAppenderConfiguration.configure(loggingLocation, excludeLoggerNames: ChocolateyLoggers.Trace.to_string()); + Log.InitializeWith(new AggregateLog(new List() { new Log4NetLog(), _logSinkLogger })); + "chocolatey".Log().Debug("XmlConfiguration is now operational"); + } _license = License.validate_license(); _container = SimpleInjectorContainer.Container; } @@ -137,8 +158,28 @@ public GetChocolatey() /// This instance public GetChocolatey SetCustomLogging(ILog logger) { - Log.InitializeWith(logger, resetLoggers: false); - drain_log_sink(logger); + return SetCustomLogging(logger, logExistingMessages: true, addToExistingLoggers: false); + } + + public GetChocolatey SetCustomLogging(ILog logger, bool logExistingMessages) + { + return SetCustomLogging(logger, logExistingMessages, addToExistingLoggers: false); + } + + public GetChocolatey SetCustomLogging(ILog logger, bool logExistingMessages, bool addToExistingLoggers) + { + var aggregateLog = new AggregateLog(new List { logger }); + if (addToExistingLoggers) + { + aggregateLog = new AggregateLog(new List { logger, Log.GetLoggerFor("chocolatey") }); + } + + Log.InitializeWith(aggregateLog, resetLoggers: false); + if (logExistingMessages) + { + drain_log_sink(logger); + } + return this; }