From 2248a7a8ddcfa08a6f468cae55ee2d1e409c6fd6 Mon Sep 17 00:00:00 2001 From: Corniel Nobel Date: Tue, 12 Nov 2024 23:12:24 +0100 Subject: [PATCH] Experimental new tracing (#286) --- src/Buildalyzer/BuildEventArgsCollector.cs | 42 ++++++++++++++++++++++ src/Buildalyzer/ProjectAnalyzer.cs | 2 ++ 2 files changed, 44 insertions(+) create mode 100644 src/Buildalyzer/BuildEventArgsCollector.cs diff --git a/src/Buildalyzer/BuildEventArgsCollector.cs b/src/Buildalyzer/BuildEventArgsCollector.cs new file mode 100644 index 0000000..20e65bd --- /dev/null +++ b/src/Buildalyzer/BuildEventArgsCollector.cs @@ -0,0 +1,42 @@ +using System.Collections.Concurrent; +using Microsoft.Build.Framework; +using Microsoft.Build.Logging; + +namespace Buildalyzer; + +[DebuggerDisplay("Count = {Count}")] +[DebuggerTypeProxy(typeof(Diagnostics.CollectionDebugView))] +internal sealed class BuildEventArgsCollector : IReadOnlyCollection, IDisposable +{ + public BuildEventArgsCollector(EventArgsDispatcher server) + { + Server = server; + Server.AnyEventRaised += EventRaised; + } + + /// + public int Count => Bag.Count; + + /// + public IEnumerator GetEnumerator() => Bag.GetEnumerator(); + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + private void EventRaised(object? sender, BuildEventArgs e) => Bag.Add(e); + + private readonly EventArgsDispatcher Server; + + private readonly ConcurrentBag Bag = []; + + public void Dispose() + { + if (!Disposed) + { + Server.AnyEventRaised -= EventRaised; + Disposed = true; + } + } + + private bool Disposed; +} diff --git a/src/Buildalyzer/ProjectAnalyzer.cs b/src/Buildalyzer/ProjectAnalyzer.cs index 348f2a5..a5429b0 100644 --- a/src/Buildalyzer/ProjectAnalyzer.cs +++ b/src/Buildalyzer/ProjectAnalyzer.cs @@ -159,6 +159,7 @@ void OnPipeLoggerOnAnyEventRaised(object o, BuildEventArgs buildEventArgs) } pipeLogger.AnyEventRaised += OnPipeLoggerOnAnyEventRaised; + using var eventProcessor = new EventProcessor(Manager, this, BuildLoggers, pipeLogger, results != null); // Run MSBuild @@ -169,6 +170,7 @@ void OnPipeLoggerOnAnyEventRaised(object o, BuildEventArgs buildEventArgs) targetsToBuild, pipeLogger.GetClientHandle(), out string arguments); + using (ProcessRunner processRunner = new ProcessRunner( fileName, arguments,