Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 2b3ade0

Browse files
AndyGerlichernguerrera
authored andcommitted
Use a multi-proc aware MSBuild logger (#8371)
Make use of the MSBuild distributed logger functionality and add a forwarding logger. When in a multi-proc build, the forwarding logger will decide which events to forward to the main node to be logged. Without this, all events are routed and a perf penalty is incurred.
1 parent ed2ca5d commit 2b3ade0

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/dotnet/commands/dotnet-msbuild/MSBuildForwardingApp.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ private static IEnumerable<string> ConcatTelemetryLogger(IEnumerable<string> arg
2828
try
2929
{
3030
Type loggerType = typeof(MSBuildLogger);
31+
Type forwardingLoggerType = typeof(MSBuildForwardingLogger);
3132

3233
return argsToForward
3334
.Concat(new[]
3435
{
35-
$"/Logger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location}"
36+
$"/distributedlogger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location}*{forwardingLoggerType.FullName},{forwardingLoggerType.GetTypeInfo().Assembly.Location}"
3637
});
3738
}
3839
catch (Exception)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Build.Framework;
5+
6+
namespace Microsoft.DotNet.Tools.MSBuild
7+
{
8+
public sealed class MSBuildForwardingLogger : IForwardingLogger
9+
{
10+
public LoggerVerbosity Verbosity { get; set; }
11+
12+
public string Parameters { get; set; }
13+
14+
public IEventRedirector BuildEventRedirector { get; set; }
15+
16+
public int NodeId { get; set; }
17+
18+
public void Initialize(IEventSource eventSource)
19+
{
20+
// Only forward telemetry events
21+
if (eventSource is IEventSource2 eventSource2)
22+
{
23+
eventSource2.TelemetryLogged += (sender, args) => BuildEventRedirector.ForwardEvent(args);
24+
}
25+
}
26+
27+
public void Initialize(IEventSource eventSource, int nodeCount)
28+
{
29+
Initialize(eventSource);
30+
}
31+
32+
public void Shutdown()
33+
{
34+
}
35+
}
36+
}

src/dotnet/commands/dotnet-msbuild/MSBuildLogger.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Microsoft.DotNet.Tools.MSBuild
1313
{
14-
public sealed class MSBuildLogger : Logger
14+
public sealed class MSBuildLogger : INodeLogger
1515
{
1616
private readonly IFirstTimeUseNoticeSentinel _sentinel =
1717
new FirstTimeUseNoticeSentinel(new CliFolderPathCalculator());
@@ -38,7 +38,12 @@ public MSBuildLogger()
3838
}
3939
}
4040

41-
public override void Initialize(IEventSource eventSource)
41+
public void Initialize(IEventSource eventSource, int nodeCount)
42+
{
43+
Initialize(eventSource);
44+
}
45+
46+
public void Initialize(IEventSource eventSource)
4247
{
4348
try
4449
{
@@ -76,7 +81,7 @@ private void OnTelemetryLogged(object sender, TelemetryEventArgs args)
7681
FormatAndSend(_telemetry, args);
7782
}
7883

79-
public override void Shutdown()
84+
public void Shutdown()
8085
{
8186
try
8287
{
@@ -86,8 +91,10 @@ public override void Shutdown()
8691
{
8792
// Exceptions during telemetry shouldn't cause anything else to fail
8893
}
89-
90-
base.Shutdown();
9194
}
95+
96+
public LoggerVerbosity Verbosity { get; set; }
97+
98+
public string Parameters { get; set; }
9299
}
93100
}

test/dotnet-msbuild.Tests/GivenDotnetMSBuildBuildsProjects.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void WhenTelemetryIsEnabledTheLoggerIsAddedToTheCommandLine()
138138
allArgs.Should().NotBeNull();
139139

140140
allArgs.Should().Contain(
141-
value => value.IndexOf("/Logger", StringComparison.OrdinalIgnoreCase) >= 0,
141+
value => value.IndexOf("/distributedlogger", StringComparison.OrdinalIgnoreCase) >= 0,
142142
"The MSBuild logger argument should be specified when telemetry is enabled.");
143143
}
144144
}

0 commit comments

Comments
 (0)