|
| 1 | +# Adding Custom Events to MSBuild |
| 2 | + |
| 3 | +MSBuild has many built-in event types, but often you may need to add a new custom one. |
| 4 | + |
| 5 | +## Steps for Binary Logger Compatibility |
| 6 | +This guide outlines the steps to ensure the BinaryLogger can work with your new event. |
| 7 | + |
| 8 | +### Changes in MSBuild |
| 9 | + |
| 10 | +1. **Add the `NewEventArgs` to `src/Framework` folder** |
| 11 | +2. **Update [BinaryLogRecordKind.cs](../../src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs)** |
| 12 | + - Append the new event to the enum |
| 13 | +3. **Modify [BinaryLogger.cs](../../src/Build/Logging/BinaryLogger/BinaryLogger.cs)** |
| 14 | + - Update `FileFormatVersion` |
| 15 | +4. **Update [BuildEventArgsReader.cs](../../src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs)** |
| 16 | + - Add a new case in the `ReadBuildEventArgs` switch |
| 17 | + - Implement a method for the added event (imitate other `ReadXYZEventArgs` methods) |
| 18 | +5. **Modify [BuildEventArgsWriter.cs](../../src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs)** |
| 19 | + - Add a new case in `WriteCore` |
| 20 | + - Document the change above the method |
| 21 | +6. **Update [LogMessagePacketBase.cs](../../src/Shared/LogMessagePacketBase.cs)** |
| 22 | + - Add to `LoggingEventType` |
| 23 | + - Add case in `GetBuildEventArgFromId` and `GetLoggingEventId` |
| 24 | +7. **Create a new test file** |
| 25 | + - Add `Framework.UnitTests/NewEventArgs_Tests.cs` |
| 26 | + - Use [BuildSubmissionStartedEventArgs_Tests.cs](../../src/Framework.UnitTests/BuildSubmissionStartedEventArgs_Tests.cs) as a reference |
| 27 | +8. **Update [NodePackets_Tests.cs](../../src/Build.UnitTests/BackEnd/NodePackets_Tests.cs)** |
| 28 | + - Add relevant test cases |
| 29 | + |
| 30 | +### Changes in [MSBuildStructuredLog](https://github.com/KirillOsenkov/MSBuildStructuredLog) |
| 31 | + |
| 32 | +1. **Update [BinaryLogRecordKind.cs](https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/master/src/StructuredLogger/BinaryLogger/BinaryLogRecordKind.cs)** |
| 33 | + - Append the new event to the enum |
| 34 | + |
| 35 | +2. **Modify [BinaryLogger.cs](https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/master/src/StructuredLogger/BinaryLogger/BinaryLogger.cs)** |
| 36 | + - Increment version |
| 37 | + - Document the change |
| 38 | + |
| 39 | +3. **Create `src/StructuredLogger/BinaryLogger/XXXEventArgs.cs`** |
| 40 | + - Implement the class for the new event (copy from MSBuild) |
| 41 | + |
| 42 | +4. **Update [BuildEventArgsReader.cs](https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/master/src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs)** |
| 43 | + - Add a new case in `ReadBuildEventArgs` |
| 44 | + |
| 45 | +5. **Modify [BuildEventArgsWriter.cs](https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/master/src/StructuredLogger/BinaryLogger/BuildEventArgsWriter.cs)** |
| 46 | + - Add a new case in `WriteCore` |
| 47 | + |
| 48 | +### Example Pull Requests adding and serializing events |
| 49 | + |
| 50 | +1. [Add Buildcheck events support + BuildSubmissionStarted](https://github.com/KirillOsenkov/MSBuildStructuredLog/pull/797) (MSBuildStructuredLog) |
| 51 | + - Corresponding [MSBuild PR](https://github.com/dotnet/msbuild/pull/10424) |
| 52 | + |
| 53 | +2. [Add Binary Logger Support for BuildCanceled](https://github.com/dotnet/msbuild/pull/10755) (MSBuild) |
| 54 | + - Corresponding [MSBuildStructuredLog PR](https://github.com/KirillOsenkov/MSBuildStructuredLog/pull/824) |
0 commit comments