11using System . Diagnostics ;
22using Microsoft . ApplicationInsights ;
3- using Microsoft . Extensions . DependencyInjection ;
43using Microsoft . Extensions . Logging ;
54
65namespace PerfProblemSimulator . Services ;
@@ -55,18 +54,22 @@ public class SimulationContext : ISimulationContext
5554{
5655 private static readonly AsyncLocal < Guid ? > _currentSimulationId = new ( ) ;
5756 private static readonly AsyncLocal < string ? > _currentSimulationType = new ( ) ;
58- private readonly IServiceProvider _serviceProvider ;
57+ private readonly TelemetryClient ? _telemetryClient ;
5958 private readonly ILogger < SimulationContext > _logger ;
6059
6160 /// <summary>
6261 /// Initializes a new instance of the <see cref="SimulationContext"/> class.
6362 /// </summary>
64- /// <param name="serviceProvider">Service provider for resolving TelemetryClient .</param>
63+ /// <param name="telemetryClient">Application Insights TelemetryClient (optional - may be null if not configured) .</param>
6564 /// <param name="logger">Logger for diagnostic output.</param>
66- public SimulationContext ( IServiceProvider serviceProvider , ILogger < SimulationContext > logger )
65+ public SimulationContext ( ILogger < SimulationContext > logger , TelemetryClient ? telemetryClient = null )
6766 {
68- _serviceProvider = serviceProvider ;
6967 _logger = logger ;
68+ _telemetryClient = telemetryClient ;
69+
70+ _logger . LogWarning (
71+ "🔧 SimulationContext initialized. TelemetryClient available: {Available}" ,
72+ _telemetryClient != null ) ;
7073 }
7174
7275 /// <inheritdoc />
@@ -122,18 +125,15 @@ public void TrackSimulationEnded(Guid simulationId, string simulationType)
122125 /// <param name="waitForTransmission">If true, blocks until transmission completes (use for CPU-intensive simulations).</param>
123126 internal void TrackSimulationEvent ( string eventName , Guid simulationId , string simulationType , bool waitForTransmission = false )
124127 {
125- _logger . LogInformation (
126- "Tracking App Insights event: {EventName} for simulation {SimulationId} ({SimulationType})" ,
128+ _logger . LogWarning (
129+ "📊 Tracking App Insights event: {EventName} for simulation {SimulationId} ({SimulationType})" ,
127130 eventName , simulationId , simulationType ) ;
128131
129132 try
130133 {
131- // Lazily resolve TelemetryClient - it may not be registered if App Insights isn't configured
132- var telemetryClient = _serviceProvider . GetService < TelemetryClient > ( ) ;
133-
134- if ( telemetryClient == null )
134+ if ( _telemetryClient == null )
135135 {
136- _logger . LogWarning ( "TelemetryClient not available (App Insights not configured), skipping event tracking" ) ;
136+ _logger . LogWarning ( "⚠️ TelemetryClient not available (App Insights not configured), skipping event tracking" ) ;
137137 return ;
138138 }
139139
@@ -143,24 +143,26 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
143143 [ "SimulationType" ] = simulationType
144144 } ;
145145
146- telemetryClient . TrackEvent ( eventName , properties ) ;
146+ _telemetryClient . TrackEvent ( eventName , properties ) ;
147+ _logger . LogWarning ( "📊 TrackEvent called for {EventName}" , eventName ) ;
147148
148149 // Flush to ensure event is sent
149- telemetryClient . Flush ( ) ;
150+ _telemetryClient . Flush ( ) ;
151+ _logger . LogWarning ( "📊 Flush called for {EventName}" , eventName ) ;
150152
151153 // For CPU-intensive operations, wait to ensure transmission completes
152154 // before background threads saturate all cores
153155 if ( waitForTransmission )
154156 {
155- _logger . LogDebug ( " Waiting for telemetry transmission to complete ...") ;
157+ _logger . LogWarning ( "📊 Waiting 1s for telemetry transmission...") ;
156158 Thread . Sleep ( 1000 ) ; // Give network I/O time to complete
157159 }
158160
159- _logger . LogDebug ( " Successfully tracked and flushed event {EventName}", eventName ) ;
161+ _logger . LogWarning ( "📊 Successfully tracked event {EventName}", eventName ) ;
160162 }
161163 catch ( Exception ex )
162164 {
163- _logger . LogError ( ex , "Failed to track App Insights event {EventName}" , eventName ) ;
165+ _logger . LogError ( ex , "❌ Failed to track App Insights event {EventName}" , eventName ) ;
164166 }
165167 }
166168
0 commit comments