|
1 | 1 | using System.Diagnostics; |
2 | 2 | using Microsoft.ApplicationInsights; |
| 3 | +using Microsoft.Extensions.Logging; |
3 | 4 |
|
4 | 5 | namespace PerfProblemSimulator.Services; |
5 | 6 |
|
@@ -37,14 +38,27 @@ public class SimulationContext : ISimulationContext |
37 | 38 | private static readonly AsyncLocal<Guid?> _currentSimulationId = new(); |
38 | 39 | private static readonly AsyncLocal<string?> _currentSimulationType = new(); |
39 | 40 | private readonly TelemetryClient? _telemetryClient; |
| 41 | + private readonly ILogger<SimulationContext> _logger; |
40 | 42 |
|
41 | 43 | /// <summary> |
42 | 44 | /// Initializes a new instance of the <see cref="SimulationContext"/> class. |
43 | 45 | /// </summary> |
44 | | - /// <param name="telemetryClient">The Application Insights telemetry client (optional).</param> |
45 | | - public SimulationContext(TelemetryClient? telemetryClient = null) |
| 46 | + /// <param name="logger">Logger for diagnostic output.</param> |
| 47 | + /// <param name="telemetryClient">The Application Insights telemetry client (optional - null when App Insights not configured).</param> |
| 48 | + public SimulationContext(ILogger<SimulationContext> logger, TelemetryClient? telemetryClient = null) |
46 | 49 | { |
| 50 | + _logger = logger; |
47 | 51 | _telemetryClient = telemetryClient; |
| 52 | + |
| 53 | + if (_telemetryClient == null) |
| 54 | + { |
| 55 | + _logger.LogWarning("SimulationContext: TelemetryClient is null - Application Insights events will not be tracked. " + |
| 56 | + "Ensure APPLICATIONINSIGHTS_CONNECTION_STRING is set."); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + _logger.LogInformation("SimulationContext: TelemetryClient initialized successfully"); |
| 61 | + } |
48 | 62 | } |
49 | 63 |
|
50 | 64 | /// <inheritdoc /> |
@@ -81,15 +95,35 @@ public IDisposable SetContext(Guid simulationId, string simulationType) |
81 | 95 | /// </summary> |
82 | 96 | internal void TrackSimulationEvent(string eventName, Guid simulationId, string simulationType) |
83 | 97 | { |
84 | | - if (_telemetryClient == null) return; |
| 98 | + _logger.LogInformation( |
| 99 | + "Tracking App Insights event: {EventName} for simulation {SimulationId} ({SimulationType})", |
| 100 | + eventName, simulationId, simulationType); |
85 | 101 |
|
86 | | - var properties = new Dictionary<string, string> |
| 102 | + if (_telemetryClient == null) |
87 | 103 | { |
88 | | - ["SimulationId"] = simulationId.ToString(), |
89 | | - ["SimulationType"] = simulationType |
90 | | - }; |
| 104 | + _logger.LogDebug("TelemetryClient is null, skipping event tracking"); |
| 105 | + return; |
| 106 | + } |
91 | 107 |
|
92 | | - _telemetryClient.TrackEvent(eventName, properties); |
| 108 | + try |
| 109 | + { |
| 110 | + var properties = new Dictionary<string, string> |
| 111 | + { |
| 112 | + ["SimulationId"] = simulationId.ToString(), |
| 113 | + ["SimulationType"] = simulationType |
| 114 | + }; |
| 115 | + |
| 116 | + _telemetryClient.TrackEvent(eventName, properties); |
| 117 | + |
| 118 | + // Flush to ensure event is sent immediately (important for short-lived simulations) |
| 119 | + _telemetryClient.Flush(); |
| 120 | + |
| 121 | + _logger.LogDebug("Successfully tracked and flushed event {EventName}", eventName); |
| 122 | + } |
| 123 | + catch (Exception ex) |
| 124 | + { |
| 125 | + _logger.LogError(ex, "Failed to track App Insights event {EventName}", eventName); |
| 126 | + } |
93 | 127 | } |
94 | 128 |
|
95 | 129 | private class ContextScope : IDisposable |
|
0 commit comments