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 21, 2021
1 parent af3e8c9 commit 9701beb
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -122,10 +128,25 @@ public class GetChocolatey
/// Initializes a new instance of the <see cref="GetChocolatey"/> class.
/// </summary>
public GetChocolatey()
: this(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 }));
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<ILog>() { new Log4NetLog(), _logSinkLogger }));
"chocolatey".Log().Debug("XmlConfiguration is now operational");
}
_license = License.validate_license();
_container = SimpleInjectorContainer.Container;
}
Expand All @@ -137,8 +158,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 9701beb

Please sign in to comment.