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; }