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

Make BuildCheckForwardingLogger filter the events #10186

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

namespace Microsoft.Build.Experimental.BuildCheck.Infrastructure;

/// <summary>
/// Central logger for the build check infrastructure.
/// Receives events from the <see cref="BuildCheckForwardingLogger"/>.
/// Processes the events and forwards them to the <see cref="IBuildCheckManager"/> and registered analyzers.
/// </summary>
/// <remarks>
/// Ensure that the consuming events are in sync with <see cref="BuildCheckForwardingLogger"/>.
/// </remarks>
internal sealed class BuildCheckConnectorLogger : ILogger
{
private readonly Dictionary<Type, Action<BuildEventArgs>> _eventHandlers;
Expand Down
68 changes: 65 additions & 3 deletions src/Build/BuildCheck/Infrastructure/BuildCheckForwardingLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,75 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Experimental.BuildCheck.Acquisition;
using Microsoft.Build.Framework;
using static Microsoft.Build.Experimental.BuildCheck.Infrastructure.BuildCheckManagerProvider;

namespace Microsoft.Build.Experimental.BuildCheck.Infrastructure;

/// <summary>
/// Forwarding logger for the build check infrastructure.
/// For now we jus want to forward all events, while disable verbose logging of tasks.
/// For now we just want to forward all events that are needed for BuildCheckConnectorLogger and filter out all other.
/// If the custom analyzer is detected, starts to unconditionally forward all events.
/// In the future we may need more specific behavior.
/// </summary>
internal class BuildCheckForwardingLogger : CentralForwardingLogger
{ }
/// <remarks>
/// Ensure that events filtering is in sync with <see cref="BuildCheckConnectorLogger"/>
/// </remarks>
internal class BuildCheckForwardingLogger : IForwardingLogger
{
public IEventRedirector? BuildEventRedirector { get; set; }

public int NodeId { get; set; }

public LoggerVerbosity Verbosity { get => LoggerVerbosity.Quiet; set { return; } }
f-alizada marked this conversation as resolved.
Show resolved Hide resolved

public string? Parameters { get; set; }
AR-May marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Set of events to be forwarded to <see cref="BuildCheckConnectorLogger"/>
/// </summary>
private HashSet<Type> _eventsToForward = new HashSet<Type>
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
{
typeof(ProjectEvaluationFinishedEventArgs),
typeof(ProjectEvaluationStartedEventArgs),
typeof(ProjectStartedEventArgs),
typeof(ProjectFinishedEventArgs),
typeof(BuildCheckTracingEventArgs),
typeof(BuildCheckAcquisitionEventArgs),
typeof(TaskStartedEventArgs),
typeof(TaskFinishedEventArgs),
typeof(TaskParameterEventArgs)
};

private bool _customAnalyzerDetected;

public void Initialize(IEventSource eventSource, int nodeCount) => Initialize(eventSource);

public void Initialize(IEventSource eventSource)
{
_customAnalyzerDetected = false;
eventSource.AnyEventRaised += EventSource_AnyEventRaised;
}

public void EventSource_AnyEventRaised(object sender, BuildEventArgs buildEvent)
{
if (_customAnalyzerDetected)
AR-May marked this conversation as resolved.
Show resolved Hide resolved
{
BuildEventRedirector?.ForwardEvent(buildEvent);
return;
}

if (_eventsToForward.Contains(buildEvent.GetType()))
{
if (buildEvent is BuildCheckAcquisitionEventArgs)
{
_customAnalyzerDetected = true;
}

BuildEventRedirector?.ForwardEvent(buildEvent);
}
}

public void Shutdown() { }
}