Skip to content

Commit b4e03d1

Browse files
Merge pull request #824 from JanProvaznik/dev/janpro/binlog-build-cancelled-event
add new event type BuildCanceled
2 parents 2f0db80 + 460f5c1 commit b4e03d1

7 files changed

+82
-5
lines changed

src/StructuredLogger/BinaryLogger/BinaryLogRecordKind.cs

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ public enum BinaryLogRecordKind
3636
BuildCheckTracing,
3737
BuildCheckAcquisition,
3838
BuildSubmissionStarted,
39+
BuildCanceled,
3940
}
4041
}

src/StructuredLogger/BinaryLogger/BinaryLogger.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ public sealed class BinaryLogger : ILogger
7676
// version 23:
7777
// - new record kinds: BuildCheckMessageEvent, BuildCheckWarningEvent, BuildCheckErrorEvent,
7878
// BuildCheckTracingEvent, BuildCheckAcquisitionEvent, BuildSubmissionStartedEvent
79+
// version 24:
80+
// - new record kind: BuildCanceledEvent
7981

8082
// This should be never changed.
8183
// The minimum version of the binary log reader that can read log of above version.
8284
internal const int ForwardCompatibilityMinimalVersion = 18;
8385

8486
// The current version of the binary log representation.
8587
// Changes with each update of the binary log format.
86-
internal const int FileFormatVersion = 23;
88+
internal const int FileFormatVersion = 24;
8789
// The minimum version of the binary log reader that can read log of above version.
8890
// This should be changed only when the binary log format is changed in a way that would prevent it from being
8991
// read by older readers. (changing of the individual BuildEventArgs or adding new is fine - as reader can
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.IO;
6+
7+
namespace StructuredLogger.BinaryLogger
8+
{
9+
/// <summary>
10+
/// This class represents the event arguments for build canceled events.
11+
/// </summary>
12+
internal sealed class BuildCanceledEventArgs : Microsoft.Build.Framework.BuildStatusEventArgs
13+
{
14+
15+
/// <summary>
16+
/// Constructor which allows the timestamp to be set.
17+
/// </summary>
18+
/// <param name="message">text message</param>
19+
/// <param name="eventTimestamp">Timestamp when the event was created</param>
20+
public BuildCanceledEventArgs(
21+
string message,
22+
DateTime eventTimestamp)
23+
: this(message, eventTimestamp, null)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Constructor which allows the timestamp to be set.
29+
/// </summary>
30+
/// <param name="message">text message</param>
31+
/// <param name="eventTimestamp">Timestamp when the event was created</param>
32+
/// <param name="messageArgs">message arguments</param>
33+
public BuildCanceledEventArgs(
34+
string message,
35+
DateTime eventTimestamp,
36+
params object[]? messageArgs)
37+
: base(message, null, "MSBuild", eventTimestamp, messageArgs)
38+
{
39+
if (string.IsNullOrWhiteSpace(message))
40+
{
41+
throw new ArgumentException("Message cannot be null or consist only white-space characters.");
42+
}
43+
}
44+
}
45+
}

src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ void HandleError(FormatErrorMessage msgFactory, bool noThrow, ReaderErrorType re
338338
BinaryLogRecordKind.BuildCheckTracing => ReadBuildCheckTracingEventArgs(),
339339
BinaryLogRecordKind.BuildCheckAcquisition => ReadBuildCheckAcquisitionEventArgs(),
340340
BinaryLogRecordKind.BuildSubmissionStarted => ReadBuildSubmissionStartedEventArgs(),
341+
BinaryLogRecordKind.BuildCanceled => ReadBuildCanceledEventArgs(),
341342
_ => null,
342343

343344
};
@@ -711,6 +712,15 @@ private BuildEventArgs ReadBuildFinishedEventArgs()
711712
SetCommonFields(e, fields);
712713
return e;
713714
}
715+
private BuildEventArgs ReadBuildCanceledEventArgs()
716+
{
717+
var fields = ReadBuildEventArgsFields();
718+
var e = new BuildCanceledEventArgs(
719+
fields.Message,
720+
fields.Timestamp);
721+
SetCommonFields(e, fields);
722+
return e;
723+
}
714724

715725
private BuildEventArgs ReadProjectEvaluationStartedEventArgs()
716726
{
@@ -1287,7 +1297,7 @@ private BuildEventArgs ReadBuildCheckAcquisitionEventArgs()
12871297
private BuildEventArgs ReadBuildSubmissionStartedEventArgs()
12881298
{
12891299
var fields = ReadBuildEventArgsFields(readImportance: false);
1290-
var e = new BuildSubmissionStartedEvent();
1300+
var e = new BuildSubmissionStartedEventArgs();
12911301

12921302
var globalProperties = ReadStringDictionary();
12931303
var entries = ReadStringList();

src/StructuredLogger/BinaryLogger/BuildEventArgsWriter.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Base types and inheritance ("EventArgs" suffix omitted):
183183
ProjectFinished
184184
BuildStarted
185185
BuildFinished
186+
BuildCanceled
186187
ProjectEvaluationStarted
187188
ProjectEvaluationFinished
188189
BuildError
@@ -211,7 +212,8 @@ private BinaryLogRecordKind WriteCore(BuildEventArgs e)
211212
case ProjectFinishedEventArgs projectFinished: return Write(projectFinished);
212213
case BuildStartedEventArgs buildStarted: return Write(buildStarted);
213214
case BuildFinishedEventArgs buildFinished: return Write(buildFinished);
214-
case BuildSubmissionStartedEvent buildSubmissionStarted: return Write(buildSubmissionStarted);
215+
case BuildCanceledEventArgs buildCanceled: return Write(buildCanceled);
216+
case BuildSubmissionStartedEventArgs buildSubmissionStarted: return Write(buildSubmissionStarted);
215217
case ProjectEvaluationStartedEventArgs projectEvaluationStarted: return Write(projectEvaluationStarted);
216218
case ProjectEvaluationFinishedEventArgs projectEvaluationFinished: return Write(projectEvaluationFinished);
217219
case BuildCheckTracingEventArgs buildCheckTracing: return Write(buildCheckTracing);
@@ -311,6 +313,12 @@ private BinaryLogRecordKind Write(BuildFinishedEventArgs e)
311313
return BinaryLogRecordKind.BuildFinished;
312314
}
313315

316+
private BinaryLogRecordKind Write(BuildCanceledEventArgs e)
317+
{
318+
WriteBuildEventArgsFields(e);
319+
return BinaryLogRecordKind.BuildCanceled;
320+
}
321+
314322
private BinaryLogRecordKind Write(ProjectEvaluationStartedEventArgs e)
315323
{
316324
WriteBuildEventArgsFields(e, writeMessage: false);
@@ -336,7 +344,7 @@ private BinaryLogRecordKind Write(BuildCheckAcquisitionEventArgs e)
336344
return BinaryLogRecordKind.BuildCheckAcquisition;
337345
}
338346

339-
private BinaryLogRecordKind Write(BuildSubmissionStartedEvent e)
347+
private BinaryLogRecordKind Write(BuildSubmissionStartedEventArgs e)
340348
{
341349
WriteBuildEventArgsFields(e, writeMessage: false);
342350
Write(e.GlobalProperties);

src/StructuredLogger/BinaryLogger/BuildSubmissionStartedEvent.cs src/StructuredLogger/BinaryLogger/BuildSubmissionStartedEventArgs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace StructuredLogger.BinaryLogger
66
{
7-
internal class BuildSubmissionStartedEvent()
7+
internal class BuildSubmissionStartedEventArgs()
88
: BuildStatusEventArgs(message: "", helpKeyword: null, senderName: null, eventTimestamp: DateTime.UtcNow)
99
{
1010
public IDictionary<string, string?> GlobalProperties { get; set; }

src/StructuredLogger/Construction/Construction.cs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Build.Collections;
99
using Microsoft.Build.Framework;
1010
using Microsoft.Build.Framework.Profiler;
11+
using StructuredLogger.BinaryLogger;
1112

1213
namespace Microsoft.Build.Logging.StructuredLogger
1314
{
@@ -583,6 +584,16 @@ void AddGlobalProperties()
583584
AddPropertiesSorted(propertiesFolder, projectEvaluation, projectEvaluationFinished.Properties);
584585
AddItems(itemsNode, projectEvaluationFinished.Items);
585586
}
587+
}
588+
else if (e is BuildCanceledEventArgs buildCanceledEventArgs)
589+
{
590+
// If the build was canceled we want to show a message in the build log view.
591+
messageProcessor.Process(new BuildMessageEventArgs(
592+
Intern(buildCanceledEventArgs.Message),
593+
Intern(buildCanceledEventArgs.HelpKeyword),
594+
Intern(buildCanceledEventArgs.SenderName),
595+
MessageImportance.High,
596+
buildCanceledEventArgs.Timestamp));
586597
}
587598
}
588599
}

0 commit comments

Comments
 (0)