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

Remove redundant creation of Forwarding logger with NullLogger #10125

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 72 additions & 11 deletions src/Build/BackEnd/BuildManager/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3019,17 +3019,9 @@ private void OnProjectStarted(object sender, ProjectStartedEventArgs e)

if (loggingService.Loggers.Count == 0)
{
// We need to register SOME logger if we don't have any. This ensures the out of proc nodes will still send us message,
// ensuring we receive project started and finished events.
LoggerDescription forwardingLoggerDescription = new LoggerDescription(
loggerClassName: typeof(ConfigurableForwardingLogger).FullName,
loggerAssemblyName: typeof(ConfigurableForwardingLogger).GetTypeInfo().Assembly.GetName().FullName,
loggerAssemblyFile: null,
loggerSwitchParameters: "PROJECTSTARTEDEVENT;PROJECTFINISHEDEVENT;FORWARDPROJECTCONTEXTEVENTS",
verbosity: LoggerVerbosity.Quiet);

ForwardingLoggerRecord[] forwardingLogger = { new ForwardingLoggerRecord(new NullLogger(), forwardingLoggerDescription) };
forwardingLoggers = forwardingLoggers?.Concat(forwardingLogger) ?? forwardingLogger;
// if no loggers have been registered - let's make sure that at least on forwarding logger
// will forward events we need (project started and finished events)
forwardingLoggers = ProcessForwardingLoggers(forwardingLoggers);
}

if (forwardingLoggers != null)
Expand All @@ -3047,6 +3039,75 @@ private void OnProjectStarted(object sender, ProjectStartedEventArgs e)
}

return loggingService;

// We need to register SOME logger if we don't have any. This ensures the out of proc nodes will still send us message,
// ensuring we receive project started and finished events.
static List<ForwardingLoggerRecord> ProcessForwardingLoggers(IEnumerable<ForwardingLoggerRecord> forwarders)
{
Type configurableLoggerType = typeof(ConfigurableForwardingLogger);
string engineAssemblyName = configurableLoggerType.GetTypeInfo().Assembly.GetName().FullName;
string configurableLoggerName = configurableLoggerType.FullName;

if (forwarders == null)
{
return [CreateMinimalForwarder()];
}

List<ForwardingLoggerRecord> result = forwarders.ToList();

// The forwarding loggers that are registered are unknown to us - we cannot make any assumptions.
// So to be on a sure side - we need to add ours.
if (result.Any(l => l.ForwardingLoggerDescription.Name.Contains(engineAssemblyName)))
{
result.Add(CreateMinimalForwarder());
return result;
}

// Those are the cases where we are sure that we have the forwarding setup as need.
if (result.Any(l =>
l.ForwardingLoggerDescription.Name.Contains(typeof(CentralForwardingLogger).FullName)
||
(l.ForwardingLoggerDescription.Name.Contains(configurableLoggerName)
&&
l.ForwardingLoggerDescription.LoggerSwitchParameters.Contains("PROJECTSTARTEDEVENT")
&&
l.ForwardingLoggerDescription.LoggerSwitchParameters.Contains("PROJECTFINISHEDEVENT")
&&
l.ForwardingLoggerDescription.LoggerSwitchParameters.Contains("FORWARDPROJECTCONTEXTEVENTS")
)))
{
return result;
}

// In case there is a ConfigurableForwardingLogger, that is not configured as we'd need - we can adjust the config
ForwardingLoggerRecord configurableLogger = result.FirstOrDefault(l =>
l.ForwardingLoggerDescription.Name.Contains(configurableLoggerName));

// If there is not - we need to add our own.
if (configurableLogger == null)
{
result.Add(CreateMinimalForwarder());
return result;
}

configurableLogger.ForwardingLoggerDescription.LoggerSwitchParameters += ";PROJECTSTARTEDEVENT;PROJECTFINISHEDEVENT;FORWARDPROJECTCONTEXTEVENTS;RESPECTVERBOSITY";

return result;

ForwardingLoggerRecord CreateMinimalForwarder()
{
// We need to register SOME logger if we don't have any. This ensures the out of proc nodes will still send us message,
// ensuring we receive project started and finished events.
LoggerDescription forwardingLoggerDescription = new LoggerDescription(
loggerClassName: configurableLoggerName,
loggerAssemblyName: engineAssemblyName,
loggerAssemblyFile: null,
loggerSwitchParameters: "PROJECTSTARTEDEVENT;PROJECTFINISHEDEVENT;FORWARDPROJECTCONTEXTEVENTS",
verbosity: LoggerVerbosity.Quiet);

return new ForwardingLoggerRecord(new NullLogger(), forwardingLoggerDescription);
}
}
}

private static void LogDeferredMessages(ILoggingService loggingService, IEnumerable<DeferredBuildMessage> deferredBuildMessages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ private void ApplyParameter(IEventSource eventSource, string parameterName)
_forwardProjectContext = true;
isEventForwardingParameter = false;
break;
case RespectVerbosityDescription:
_respectVerbosity = true;
isEventForwardingParameter = false;
break;
default:
isEventForwardingParameter = false;
break;
Expand All @@ -202,7 +206,7 @@ public virtual void Initialize(IEventSource eventSource)

ParseParameters(eventSource);

if (!_forwardingSetFromParameters)
if (_respectVerbosity || !_forwardingSetFromParameters)
{
SetForwardingBasedOnVerbosity(eventSource);
}
Expand Down Expand Up @@ -421,6 +425,7 @@ private bool IsVerbosityAtLeast(LoggerVerbosity checkVerbosity)
private const string NoSummaryDescription = "NOSUMMARY";
private const string ShowCommandLineDescription = "SHOWCOMMANDLINE";
private const string ForwardProjectContextDescription = "FORWARDPROJECTCONTEXTEVENTS";
private const string RespectVerbosityDescription = "RESPECTVERBOSITY";

#region Per-build Members

Expand All @@ -435,6 +440,12 @@ private bool IsVerbosityAtLeast(LoggerVerbosity checkVerbosity)
/// </summary>
private bool _forwardingSetFromParameters;

/// <summary>
/// Indicates if the parameters explicitly specified respecting of the verbosity (forwarding will
/// be set based on verbosity, in addition to explicitly configured forwarding via parameters).
/// </summary>
private bool _respectVerbosity;

/// <summary>
/// Indicates if the events to forward should include project context events, if not
/// overridden by individual-event forwarding in <see cref="_forwardingSetFromParameters"/>.
Expand Down
4 changes: 4 additions & 0 deletions src/Build/Logging/LoggerDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public string LoggerSwitchParameters
{
return _loggerSwitchParameters;
}
internal set
{
_loggerSwitchParameters = value;
}
}

public bool IsOptional
Expand Down