|
3 | 3 |
|
4 | 4 | #nullable disable
|
5 | 5 |
|
| 6 | +using Microsoft.Build.Evaluation; |
6 | 7 | using Microsoft.Build.Execution;
|
| 8 | +using Microsoft.Build.Framework; |
7 | 9 | using Microsoft.DotNet.Cli.Extensions;
|
8 | 10 | using Microsoft.DotNet.Cli.Telemetry;
|
9 | 11 |
|
@@ -103,4 +105,97 @@ public void BuildWithTelemetry_WhenTelemetryEnabled_CreatesDistributedLogger()
|
103 | 105 | Telemetry.Telemetry.CurrentSessionId = originalSessionId;
|
104 | 106 | }
|
105 | 107 | }
|
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void TelemetryLogger_ReceivesEventsFromAPIBasedBuild() |
| 111 | + { |
| 112 | + // Enable telemetry with a session ID |
| 113 | + var originalSessionId = Telemetry.Telemetry.CurrentSessionId; |
| 114 | + try |
| 115 | + { |
| 116 | + Telemetry.Telemetry.CurrentSessionId = Guid.NewGuid().ToString(); |
| 117 | + |
| 118 | + // Create a simple in-memory project |
| 119 | + string projectContent = @" |
| 120 | +<Project> |
| 121 | + <Target Name='TestTarget'> |
| 122 | + <Message Text='Test message' Importance='high' /> |
| 123 | + </Target> |
| 124 | +</Project>"; |
| 125 | + |
| 126 | + // Create ProjectCollection with telemetry logger |
| 127 | + var (loggers, telemetryCentralLogger) = ProjectInstanceExtensions.CreateLoggersWithTelemetry(); |
| 128 | + using var collection = new ProjectCollection( |
| 129 | + globalProperties: null, |
| 130 | + loggers: loggers, |
| 131 | + toolsetDefinitionLocations: ToolsetDefinitionLocations.Default); |
| 132 | + |
| 133 | + // Create a temporary project file |
| 134 | + var tempFile = Path.GetTempFileName(); |
| 135 | + try |
| 136 | + { |
| 137 | + File.WriteAllText(tempFile, projectContent); |
| 138 | + |
| 139 | + // Load and build the project using API-based MSBuild with telemetry |
| 140 | + var project = collection.LoadProject(tempFile); |
| 141 | + var projectInstance = project.CreateProjectInstance(); |
| 142 | + |
| 143 | + // Use a test logger to capture events |
| 144 | + var testLogger = new TestEventLogger(); |
| 145 | + |
| 146 | + // Build directly without distributed logger for simpler test |
| 147 | + // The telemetry logger is already attached to the ProjectCollection |
| 148 | + var result = projectInstance.Build(new[] { "TestTarget" }, new[] { testLogger }); |
| 149 | + |
| 150 | + // Verify build succeeded |
| 151 | + result.Should().BeTrue(); |
| 152 | + |
| 153 | + // Verify the test logger received events (indicating build actually ran) |
| 154 | + testLogger.BuildStartedCount.Should().BeGreaterThan(0); |
| 155 | + testLogger.BuildFinishedCount.Should().BeGreaterThan(0); |
| 156 | + |
| 157 | + // Verify telemetry logger was created and attached to collection |
| 158 | + telemetryCentralLogger.Should().NotBeNull(); |
| 159 | + loggers.Should().Contain(telemetryCentralLogger); |
| 160 | + } |
| 161 | + finally |
| 162 | + { |
| 163 | + if (File.Exists(tempFile)) |
| 164 | + { |
| 165 | + File.Delete(tempFile); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + finally |
| 170 | + { |
| 171 | + // Restore original session ID |
| 172 | + Telemetry.Telemetry.CurrentSessionId = originalSessionId; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Simple logger to track build events for testing |
| 178 | + /// </summary> |
| 179 | + private class TestEventLogger : ILogger |
| 180 | + { |
| 181 | + public int BuildStartedCount { get; private set; } |
| 182 | + public int BuildFinishedCount { get; private set; } |
| 183 | + public int TargetStartedCount { get; private set; } |
| 184 | + public int TargetFinishedCount { get; private set; } |
| 185 | + |
| 186 | + public LoggerVerbosity Verbosity { get; set; } |
| 187 | + public string Parameters { get; set; } |
| 188 | + |
| 189 | + public void Initialize(IEventSource eventSource) |
| 190 | + { |
| 191 | + eventSource.BuildStarted += (sender, e) => BuildStartedCount++; |
| 192 | + eventSource.BuildFinished += (sender, e) => BuildFinishedCount++; |
| 193 | + eventSource.TargetStarted += (sender, e) => TargetStartedCount++; |
| 194 | + eventSource.TargetFinished += (sender, e) => TargetFinishedCount++; |
| 195 | + } |
| 196 | + |
| 197 | + public void Shutdown() |
| 198 | + { |
| 199 | + } |
| 200 | + } |
106 | 201 | }
|
0 commit comments