Skip to content

Commit f600c5e

Browse files
authored
Fix build output logging in .NET 10 (#343)
1 parent 1cce1f8 commit f600c5e

File tree

12 files changed

+283
-82
lines changed

12 files changed

+283
-82
lines changed

Directory.Packages.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
5-
<MicrosoftBuildPackageVersion>17.14.8</MicrosoftBuildPackageVersion>
6-
<MicrosoftBuildPackageVersion Condition="'$(TargetFramework)' == 'net8.0' ">17.11.4</MicrosoftBuildPackageVersion>
5+
<MicrosoftBuildPackageVersion>17.14.28</MicrosoftBuildPackageVersion>
6+
<MicrosoftBuildPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">17.11.48</MicrosoftBuildPackageVersion>
7+
<MicrosoftBuildPackageVersion Condition="'$(TargetFramework)' == 'net10.0'">17.15.0-preview-25277-114</MicrosoftBuildPackageVersion>
78
</PropertyGroup>
89
<ItemGroup>
910
<PackageVersion Include="AssemblyShader" Version="1.1.3" />

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/BuildEngineTests.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,31 @@ public void ConsoleLog()
1616
{
1717
BuildEngine buildEngine = GetBuildEngineWithEvents(i =>
1818
{
19-
i.LogErrorEvent(new BuildErrorEventArgs(null, "A6DAB901460D483FBDF3A0980B14C46F", "48F7F352E2914991827100BCEB69331F", 3, 4, 0, 0, "D988473FF8634A16A0CD8FE94FF20D53", null, null));
20-
i.LogWarningEvent(new BuildWarningEventArgs(null, "AE1B25881A694A70B2EA299C04625596", "07006F38A63E420AAB4124EBE58081BC", 1, 2, 0, 0, "3A3DD4A40DA44BA5BBB123E105EE1F71", null, null));
21-
i.LogMessageEvent(new BuildMessageEventArgs("61BD637C7D704D4B98C25805E3111152", null, null, MessageImportance.High));
22-
i.LogMessageEvent(new BuildMessageEventArgs("B02496FA4D3348A6997DC918EBF7455B", null, null, MessageImportance.Normal));
23-
i.LogMessageEvent(new BuildMessageEventArgs("2C254C4346A347AE94AE5E7FB6C03B0C", null, null, MessageImportance.Low));
19+
i.LogErrorEvent(
20+
new BuildErrorEventArgs(null, "A6DAB901460D483FBDF3A0980B14C46F", "48F7F352E2914991827100BCEB69331F", 3, 4, 0, 0, "D988473FF8634A16A0CD8FE94FF20D53", null, null)
21+
{
22+
BuildEventContext = BuildEventContext.Invalid,
23+
});
24+
i.LogWarningEvent(
25+
new BuildWarningEventArgs(null, "AE1B25881A694A70B2EA299C04625596", "07006F38A63E420AAB4124EBE58081BC", 1, 2, 0, 0, "3A3DD4A40DA44BA5BBB123E105EE1F71", null, null)
26+
{
27+
BuildEventContext = BuildEventContext.Invalid,
28+
});
29+
i.LogMessageEvent(
30+
new BuildMessageEventArgs("61BD637C7D704D4B98C25805E3111152", null, null, MessageImportance.High)
31+
{
32+
BuildEventContext = BuildEventContext.Invalid,
33+
});
34+
i.LogMessageEvent(
35+
new BuildMessageEventArgs("B02496FA4D3348A6997DC918EBF7455B", null, null, MessageImportance.Normal)
36+
{
37+
BuildEventContext = BuildEventContext.Invalid,
38+
});
39+
i.LogMessageEvent(
40+
new BuildMessageEventArgs("2C254C4346A347AE94AE5E7FB6C03B0C", null, null, MessageImportance.Low)
41+
{
42+
BuildEventContext = BuildEventContext.Invalid,
43+
});
2444
});
2545

2646
buildEngine.GetConsoleLog()

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/MockEventSource.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,44 @@ public void OnBuildFinished(bool succeeded, string? message = null, string? help
4747

4848
public void OnErrorRaised(string message, string? code = null, string? file = null, int lineNumber = -1, int columnNumber = -1, int endLineNumber = -1, int endColumnNumber = -1, string? helpKeyword = null, string? senderName = null)
4949
{
50-
BuildErrorEventArgs args = new BuildErrorEventArgs(null, code, file, lineNumber, columnNumber, endLineNumber, endColumnNumber, message, helpKeyword, senderName);
50+
BuildErrorEventArgs args = new BuildErrorEventArgs(null, code, file, lineNumber, columnNumber, endLineNumber, endColumnNumber, message, helpKeyword, senderName)
51+
{
52+
BuildEventContext = BuildEventContext.Invalid,
53+
};
54+
5155
ErrorRaised?.Invoke(this, args);
5256
OnAnyEventRaised(args);
5357
}
5458

5559
public void OnMessageRaised(string message, MessageImportance importance = MessageImportance.Normal)
5660
{
57-
BuildMessageEventArgs args = new BuildMessageEventArgs(message, null, null, importance);
61+
BuildMessageEventArgs args = new BuildMessageEventArgs(message, null, null, importance)
62+
{
63+
BuildEventContext = BuildEventContext.Invalid,
64+
};
65+
5866
MessageRaised?.Invoke(this, args);
5967
OnAnyEventRaised(args);
6068
}
6169

6270
public void OnProjectFinished(string projectFile, bool succeeded, string? message = null, string? helpKeyword = null)
6371
{
64-
ProjectFinishedEventArgs args = new ProjectFinishedEventArgs(message, helpKeyword, projectFile, succeeded);
72+
ProjectFinishedEventArgs args = new ProjectFinishedEventArgs(message, helpKeyword, projectFile, succeeded)
73+
{
74+
BuildEventContext = BuildEventContext.Invalid,
75+
};
76+
6577
ProjectFinished?.Invoke(this, args);
6678
OnAnyEventRaised(args);
6779
}
6880

6981
public void OnWarningRaised(string message, string? code = null, string? file = null, int lineNumber = -1, int columnNumber = -1, int endLineNumber = -1, int endColumnNumber = -1, string? helpKeyword = null, string? senderName = null)
7082
{
71-
BuildWarningEventArgs args = new BuildWarningEventArgs(null, code, file, lineNumber, columnNumber, endLineNumber, endColumnNumber, message, helpKeyword, senderName);
83+
BuildWarningEventArgs args = new BuildWarningEventArgs(null, code, file, lineNumber, columnNumber, endLineNumber, endColumnNumber, message, helpKeyword, senderName)
84+
{
85+
BuildEventContext = BuildEventContext.Invalid,
86+
};
87+
7288
WarningRaised?.Invoke(this, args);
7389
OnAnyEventRaised(args);
7490
}

src/Microsoft.Build.Utilities.ProjectCreation/BuildEventArgsCollection.cs

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Licensed under the MIT license.
44

5+
using Microsoft.Build.Evaluation;
56
using Microsoft.Build.Framework;
67
using Microsoft.Build.Logging;
78
using System;
@@ -108,8 +109,16 @@ public virtual void Dispose()
108109
/// Gets the current build output in the format of a console log.
109110
/// </summary>
110111
/// <param name="verbosity">The logger verbosity to use.</param>
112+
/// <param name="showSummary">Optional parameter indicating whether or not to show an error and warning summary at the end.</param>
113+
/// <param name="performanceSummary">Optional parameter indicating whether or not to show time spent in tasks, targets and projects.</param>
114+
/// <param name="errorsOnly">Optional parameter indicating whether or not to show only errors.</param>
115+
/// <param name="warningsOnly">Optional parameter indicating whether or not to show only warnings.</param>
116+
/// <param name="showItemAndPropertyList">Optional parameter indicating whether or not to show a list of items and properties at the start of each project build.</param>
117+
/// <param name="showCommandLine">Optional parameter indicating whether or not to show <see cref="TaskCommandLineEventArgs" /> messages.</param>
118+
/// <param name="showTimestamp">Optional parameter indicating whether or not to show the timestamp as a prefix to any message.</param>
119+
/// <param name="showEventId">Optional parameter indicating whether or not to show eventId for started events, finished events, and messages.</param>
111120
/// <returns>The build output in the format of a console log.</returns>
112-
public string GetConsoleLog(LoggerVerbosity verbosity = LoggerVerbosity.Normal)
121+
public string GetConsoleLog(LoggerVerbosity verbosity = LoggerVerbosity.Normal, bool showSummary = true, bool performanceSummary = false, bool errorsOnly = false, bool warningsOnly = false, bool showItemAndPropertyList = true, bool showCommandLine = false, bool showTimestamp = false, bool showEventId = false)
113122
{
114123
if (_lastConsoleOutput != null && verbosity == _lastVerbosity)
115124
{
@@ -118,65 +127,7 @@ public string GetConsoleLog(LoggerVerbosity verbosity = LoggerVerbosity.Normal)
118127

119128
_lastVerbosity = verbosity;
120129

121-
StringBuilder sb = new StringBuilder(AllEvents.Count * 300);
122-
123-
ConsoleLogger logger = new ConsoleLogger(verbosity, message => sb.Append(message), _ => { }, () => { });
124-
125-
foreach (BuildEventArgs buildEventArgs in AllEvents)
126-
{
127-
switch (buildEventArgs)
128-
{
129-
case BuildMessageEventArgs buildMessageEventArgs:
130-
logger.MessageHandler(logger, buildMessageEventArgs);
131-
break;
132-
133-
case BuildErrorEventArgs buildErrorEventArgs:
134-
logger.ErrorHandler(logger, buildErrorEventArgs);
135-
break;
136-
137-
case BuildWarningEventArgs buildWarningEventArgs:
138-
logger.WarningHandler(logger, buildWarningEventArgs);
139-
break;
140-
141-
case BuildStartedEventArgs buildStartedEventArgs:
142-
logger.BuildStartedHandler(logger, buildStartedEventArgs);
143-
break;
144-
145-
case BuildFinishedEventArgs buildFinishedEventArgs:
146-
logger.BuildFinishedHandler(logger, buildFinishedEventArgs);
147-
break;
148-
149-
case ProjectStartedEventArgs projectStartedEventArgs:
150-
logger.ProjectStartedHandler(logger, projectStartedEventArgs);
151-
break;
152-
153-
case ProjectFinishedEventArgs projectFinishedEventArgs:
154-
logger.ProjectFinishedHandler(logger, projectFinishedEventArgs);
155-
break;
156-
157-
case TargetStartedEventArgs targetStartedEventArgs:
158-
logger.TargetStartedHandler(logger, targetStartedEventArgs);
159-
break;
160-
161-
case TargetFinishedEventArgs targetFinishedEventArgs:
162-
logger.TargetFinishedHandler(logger, targetFinishedEventArgs);
163-
break;
164-
165-
case TaskStartedEventArgs taskStartedEventArgs:
166-
logger.TaskStartedHandler(logger, taskStartedEventArgs);
167-
break;
168-
169-
case TaskFinishedEventArgs taskFinishedEventArgs:
170-
logger.TaskFinishedHandler(logger, taskFinishedEventArgs);
171-
break;
172-
173-
case CustomBuildEventArgs customBuildEventArgs:
174-
logger.CustomEventHandler(logger, customBuildEventArgs);
175-
break;
176-
}
177-
}
178-
179-
_lastConsoleOutput = sb.ToString();
130+
_lastConsoleOutput = ConsoleLoggerStringBuilder.GetConsoleLogAsString(AllEvents, verbosity, showSummary, performanceSummary, errorsOnly, warningsOnly, showItemAndPropertyList, showCommandLine, showTimestamp, showEventId);
180131

181132
ConsoleOutputCreated?.Invoke(this, verbosity);
182133

@@ -209,4 +160,4 @@ protected void Add(BuildEventArgs buildEventArgs)
209160
}
210161
}
211162
}
212-
}
163+
}

0 commit comments

Comments
 (0)