Skip to content

Commit

Permalink
(chocolatey#2124) Add ability to initialize without logging
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AdmiringWorm committed Jun 23, 2021
1 parent af3e8c9 commit f65b9be
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using infrastructure.licensing;
using SimpleInjector;
Expand Down Expand Up @@ -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(initializeLogging: true);
}

public static GetChocolatey GetChocolatey(bool initializeLogging)
{
return GlobalMutex.enter(() => set_up(initializeLogging), 10);
}

private static ResolveEventHandler _handler = null;
Expand Down Expand Up @@ -122,12 +128,24 @@ public class GetChocolatey
/// Initializes a new instance of the <see cref="GetChocolatey"/> class.
/// </summary>
public GetChocolatey()
: this(initializeLogging: true)
{
}

public GetChocolatey(bool initializeLogging)
{
Log4NetAppenderConfiguration.configure(null, excludeLoggerNames: ChocolateyLoggers.Trace.to_string());
Bootstrap.initialize();
Log.InitializeWith(new AggregateLog(new List<ILog>() { new Log4NetLog(), _logSinkLogger }));
_license = License.validate_license();
_container = SimpleInjectorContainer.Container;
if (initializeLogging)
{
string loggingLocation = ApplicationParameters.LoggingLocation;
var fileSystem = _container.GetInstance<IFileSystem>();
fileSystem.create_directory_if_not_exists(loggingLocation);

Log4NetAppenderConfiguration.configure(loggingLocation, excludeLoggerNames: ChocolateyLoggers.Trace.to_string());
Log.InitializeWith(new AggregateLog(new List<ILog>() { new Log4NetLog(), _logSinkLogger }));
"chocolatey".Log().Debug("XmlConfiguration is now operational");
}
_license = License.validate_license();
}

/// <summary>
Expand All @@ -137,8 +155,28 @@ public GetChocolatey()
/// <returns>This <see cref="GetChocolatey"/> instance</returns>
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<ILog> { logger });
if (addToExistingLoggers)
{
aggregateLog = new AggregateLog(new List<ILog> { logger, Log.GetLoggerFor("chocolatey") });
}

Log.InitializeWith(aggregateLog, resetLoggers: false);
if (logExistingMessages)
{
drain_log_sink(logger);
}

return this;
}

Expand Down

0 comments on commit f65b9be

Please sign in to comment.