Skip to content

Commit

Permalink
Merge pull request #2295 from AdmiringWorm/logging-append
Browse files Browse the repository at this point in the history
(#2124) Add ability to initialize without logging
  • Loading branch information
gep13 authored Aug 9, 2021
2 parents 21860db + 6200664 commit 6feee97
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 6feee97

Please sign in to comment.