Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#2124) Add ability to initialize without logging #2295

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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