diff --git a/src/Build.UnitTests/BackEnd/NodePackets_Tests.cs b/src/Build.UnitTests/BackEnd/NodePackets_Tests.cs index f609a693cba..c249a953174 100644 --- a/src/Build.UnitTests/BackEnd/NodePackets_Tests.cs +++ b/src/Build.UnitTests/BackEnd/NodePackets_Tests.cs @@ -79,6 +79,7 @@ public void VerifyEventType() GeneratedFileUsedEventArgs generatedFileUsed = new GeneratedFileUsedEventArgs("path", "some content"); BuildSubmissionStartedEventArgs buildSubmissionStarted = new(new Dictionary { { "Value1", "Value2" } }, ["Path1"], ["TargetName"], BuildRequestDataFlags.ReplaceExistingProjectInstance, 123); BuildCheckTracingEventArgs buildCheckTracing = new(); + BuildCanceledEventArgs buildCanceled = new("message", DateTime.UtcNow); VerifyLoggingPacket(buildFinished, LoggingEventType.BuildFinishedEvent); VerifyLoggingPacket(buildStarted, LoggingEventType.BuildStartedEvent); @@ -114,6 +115,7 @@ public void VerifyEventType() VerifyLoggingPacket(generatedFileUsed, LoggingEventType.GeneratedFileUsedEvent); VerifyLoggingPacket(buildSubmissionStarted, LoggingEventType.BuildSubmissionStartedEvent); VerifyLoggingPacket(buildCheckTracing, LoggingEventType.BuildCheckTracingEvent); + VerifyLoggingPacket(buildCanceled, LoggingEventType.BuildCanceledEvent); } private static BuildEventContext CreateBuildEventContext() diff --git a/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs b/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs index bf2af7ca1fc..c45baa49e1e 100644 --- a/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs +++ b/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs @@ -97,6 +97,18 @@ public void RoundtripBuildFinishedEventArgs() e => e.Succeeded.ToString()); } + [Fact] + public void RoundtripBuildCanceledEventArgs() + { + var args = new BuildCanceledEventArgs( + "Message", + eventTimestamp: DateTime.Parse("12/12/2015 06:11:56 PM")); + + Roundtrip(args, + e => e.Message, + e => e.Timestamp.ToString()); + } + [Fact] public void RoundtripBuildSubmissionStartedEventArgs() { diff --git a/src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs b/src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs index afda13dd6e5..59a7a4ef914 100644 --- a/src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs +++ b/src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs @@ -46,5 +46,6 @@ public enum BinaryLogRecordKind BuildCheckTracing, BuildCheckAcquisition, BuildSubmissionStarted, + BuildCanceled, } } diff --git a/src/Build/Logging/BinaryLogger/BinaryLogger.cs b/src/Build/Logging/BinaryLogger/BinaryLogger.cs index 0e07217a267..be4eaa2288d 100644 --- a/src/Build/Logging/BinaryLogger/BinaryLogger.cs +++ b/src/Build/Logging/BinaryLogger/BinaryLogger.cs @@ -78,6 +78,8 @@ public sealed class BinaryLogger : ILogger // version 23: // - new record kinds: BuildCheckMessageEvent, BuildCheckWarningEvent, BuildCheckErrorEvent, // BuildCheckTracingEvent, BuildCheckAcquisitionEvent, BuildSubmissionStartedEvent + // version 24: + // - new record kind: BuildCanceledEventArgs // MAKE SURE YOU KEEP BuildEventArgsWriter AND StructuredLogViewer.BuildEventArgsWriter IN SYNC WITH THE CHANGES ABOVE. // Both components must stay in sync to avoid issues with logging or event handling in the products. @@ -88,7 +90,7 @@ public sealed class BinaryLogger : ILogger // The current version of the binary log representation. // Changes with each update of the binary log format. - internal const int FileFormatVersion = 23; + internal const int FileFormatVersion = 24; // The minimum version of the binary log reader that can read log of above version. // This should be changed only when the binary log format is changed in a way that would prevent it from being diff --git a/src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs b/src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs index dc7b097be1a..54b4e2fcb9a 100644 --- a/src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs +++ b/src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs @@ -325,6 +325,7 @@ void HandleError(FormatErrorMessage msgFactory, bool noThrow, ReaderErrorType re BinaryLogRecordKind.BuildCheckError => ReadBuildErrorEventArgs(), BinaryLogRecordKind.BuildCheckTracing => ReadBuildCheckTracingEventArgs(), BinaryLogRecordKind.BuildCheckAcquisition => ReadBuildCheckAcquisitionEventArgs(), + BinaryLogRecordKind.BuildCanceled => ReadBuildCanceledEventArgs(), _ => null }; @@ -1275,6 +1276,15 @@ private BuildEventArgs ReadBuildCheckAcquisitionEventArgs() return e; } + private BuildEventArgs ReadBuildCanceledEventArgs() + { + var fields = ReadBuildEventArgsFields(); + var e = new BuildCanceledEventArgs(fields.Message); + SetCommonFields(e, fields); + + return e; + } + /// /// For errors and warnings these 8 fields are written out explicitly /// (their presence is not marked as a bit in the flags). So we have to diff --git a/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs b/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs index f140698b70a..77121b29b21 100644 --- a/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs +++ b/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs @@ -187,6 +187,7 @@ Base types and inheritance ("EventArgs" suffix omitted): BuildSubmissionStarted BuildStarted BuildFinished + BuildCanceled ProjectEvaluationStarted ProjectEvaluationFinished BuildError @@ -215,6 +216,7 @@ private BinaryLogRecordKind WriteCore(BuildEventArgs e) case BuildSubmissionStartedEventArgs buildSubmissionStarted: return Write(buildSubmissionStarted); case BuildStartedEventArgs buildStarted: return Write(buildStarted); case BuildFinishedEventArgs buildFinished: return Write(buildFinished); + case BuildCanceledEventArgs buildCanceled: return Write(buildCanceled); case ProjectEvaluationStartedEventArgs projectEvaluationStarted: return Write(projectEvaluationStarted); case ProjectEvaluationFinishedEventArgs projectEvaluationFinished: return Write(projectEvaluationFinished); case BuildCheckTracingEventArgs buildCheckTracing: return Write(buildCheckTracing); @@ -307,6 +309,13 @@ private BinaryLogRecordKind Write(BuildFinishedEventArgs e) return BinaryLogRecordKind.BuildFinished; } + private BinaryLogRecordKind Write(BuildCanceledEventArgs e) + { + WriteBuildEventArgsFields(e); + + return BinaryLogRecordKind.BuildCanceled; + } + private BinaryLogRecordKind Write(ProjectEvaluationStartedEventArgs e) { WriteBuildEventArgsFields(e, writeMessage: false); diff --git a/src/Framework.UnitTests/BuildCanceledEventArgs_Tests.cs b/src/Framework.UnitTests/BuildCanceledEventArgs_Tests.cs new file mode 100644 index 00000000000..0ad43127a32 --- /dev/null +++ b/src/Framework.UnitTests/BuildCanceledEventArgs_Tests.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Shouldly; +using Xunit; + +namespace Microsoft.Build.Framework.UnitTests +{ + public class BuildCanceledEventArgs_Tests + { + [Fact] + public void SerializationDeserializationTest() + { + var message = "message"; + var datetime = DateTime.Today; + + BuildCanceledEventArgs args = new( + message, + datetime + ); + using MemoryStream stream = new MemoryStream(); + using BinaryWriter bw = new BinaryWriter(stream); + args.WriteToStream(bw); + + stream.Position = 0; + using BinaryReader br = new BinaryReader(stream); + BuildCanceledEventArgs argDeserialized = new("m"); + int packetVersion = (Environment.Version.Major * 10) + Environment.Version.Minor; + + argDeserialized.CreateFromStream(br, packetVersion); + argDeserialized.Message.ShouldBe(message); + argDeserialized.Timestamp.ShouldBe(datetime); + } + } +} diff --git a/src/Framework.UnitTests/BuildSubmissionStartedEventAgs_Tests.cs b/src/Framework.UnitTests/BuildSubmissionStartedEventArgs_Tests.cs similarity index 97% rename from src/Framework.UnitTests/BuildSubmissionStartedEventAgs_Tests.cs rename to src/Framework.UnitTests/BuildSubmissionStartedEventArgs_Tests.cs index 1db4853b515..199d2fc038d 100644 --- a/src/Framework.UnitTests/BuildSubmissionStartedEventAgs_Tests.cs +++ b/src/Framework.UnitTests/BuildSubmissionStartedEventArgs_Tests.cs @@ -12,7 +12,7 @@ namespace Microsoft.Build.Framework.UnitTests { - public class BuildSubmissionStartedEventAgs_Tests + public class BuildSubmissionStartedEventArgs_Tests { [Fact] public void SerializationDeserializationTest() diff --git a/src/Shared/LogMessagePacketBase.cs b/src/Shared/LogMessagePacketBase.cs index 4caf4874652..610f0e53f29 100644 --- a/src/Shared/LogMessagePacketBase.cs +++ b/src/Shared/LogMessagePacketBase.cs @@ -244,6 +244,11 @@ internal enum LoggingEventType : int /// Event is . /// BuildSubmissionStartedEvent = 40, + + /// + /// Event is + /// + BuildCanceledEvent = 41, } #endregion @@ -656,6 +661,7 @@ private BuildEventArgs GetBuildEventArgFromId() LoggingEventType.BuildCheckTracingEvent => new BuildCheckTracingEventArgs(), LoggingEventType.EnvironmentVariableReadEvent => new EnvironmentVariableReadEventArgs(), LoggingEventType.BuildSubmissionStartedEvent => new BuildSubmissionStartedEventArgs(), + LoggingEventType.BuildCanceledEvent => new BuildCanceledEventArgs("Build canceled."), #endif _ => throw new InternalErrorException("Should not get to the default of GetBuildEventArgFromId ID: " + _eventType) }; @@ -799,6 +805,10 @@ private LoggingEventType GetLoggingEventId(BuildEventArgs eventArg) { return LoggingEventType.BuildSubmissionStartedEvent; } + else if (eventType == typeof(BuildCanceledEventArgs)) + { + return LoggingEventType.BuildCanceledEvent; + } #endif else if (eventType == typeof(TargetStartedEventArgs)) {