Skip to content

Commit e3688e3

Browse files
committed
app insights debugging 6
1 parent 4325c06 commit e3688e3

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

‎src/PerfProblemSimulator/Controllers/AdminController.cs‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,37 +146,61 @@ public IActionResult TestAppInsights()
146146
// Send a test event via TelemetryClient
147147
if (_telemetryClient != null && diagnostics.TelemetryClientEnabled)
148148
{
149+
var errors = new List<string>();
150+
151+
// Step 1: Simple TrackEvent (no properties)
149152
try
150153
{
151-
// Try simplest possible TrackEvent first (no properties)
152154
_telemetryClient.TrackEvent("TestEvent_Simple");
153155
diagnostics.TestEventSent = true;
154-
155-
// Now try with properties
156+
}
157+
catch (Exception ex)
158+
{
159+
errors.Add($"TrackEvent(simple): {ex.GetType().Name}: {ex.Message}");
160+
}
161+
162+
// Step 2: TrackEvent with properties
163+
try
164+
{
156165
var props = new Dictionary<string, string>
157166
{
158167
["TestId"] = testId.ToString(),
159168
["Source"] = "AdminController"
160169
};
161170
_telemetryClient.TrackEvent("TestEvent_WithProps", props);
162-
163-
// Also try TrackTrace
171+
}
172+
catch (Exception ex)
173+
{
174+
errors.Add($"TrackEvent(props): {ex.GetType().Name}: {ex.Message}");
175+
}
176+
177+
// Step 3: TrackTrace
178+
try
179+
{
164180
_telemetryClient.TrackTrace($"TestTrace - TestId: {testId}");
165-
181+
}
182+
catch (Exception ex)
183+
{
184+
errors.Add($"TrackTrace: {ex.GetType().Name}: {ex.Message}");
185+
}
186+
187+
// Step 4: Flush
188+
try
189+
{
166190
_telemetryClient.Flush();
167191
Thread.Sleep(500);
168-
169-
_logger.LogWarning("🧪 TEST EVENTS sent - TestId: {TestId}", testId);
170192
}
171193
catch (Exception ex)
172194
{
173-
diagnostics.Error = $"Telemetry threw: {ex.GetType().Name}: {ex.Message}";
174-
// Include inner exception if present
175-
if (ex.InnerException != null)
176-
{
177-
diagnostics.Error += $" Inner: {ex.InnerException.Message}";
178-
}
195+
errors.Add($"Flush: {ex.GetType().Name}: {ex.Message}");
196+
}
197+
198+
if (errors.Count > 0)
199+
{
200+
diagnostics.Error = string.Join(" | ", errors);
179201
}
202+
203+
_logger.LogWarning("🧪 TEST complete - TestId: {TestId}, Errors: {ErrorCount}", testId, errors.Count);
180204
}
181205
else
182206
{

‎src/PerfProblemSimulator/Services/SimulationContext.cs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void TrackSimulationEnded(Guid simulationId, string simulationType)
122122
/// <param name="eventName">The event name (SimulationStarted/SimulationEnded).</param>
123123
/// <param name="simulationId">The simulation ID.</param>
124124
/// <param name="simulationType">The simulation type.</param>
125-
/// <param name="waitForTransmission">If true, blocks until transmission completes (use for CPU-intensive simulations).</param>
125+
/// <param name="waitForTransmission">If true, waits briefly to allow transmission (use for CPU-intensive simulations).</param>
126126
internal void TrackSimulationEvent(string eventName, Guid simulationId, string simulationType, bool waitForTransmission = false)
127127
{
128128
_logger.LogWarning(
@@ -133,7 +133,7 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
133133
{
134134
if (_telemetryClient == null)
135135
{
136-
_logger.LogWarning("⚠️ TelemetryClient not available (App Insights not configured), skipping event tracking");
136+
_logger.LogWarning("⚠️ TelemetryClient not available, skipping event tracking");
137137
return;
138138
}
139139

@@ -146,16 +146,15 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
146146
_telemetryClient.TrackEvent(eventName, properties);
147147
_logger.LogWarning("📊 TrackEvent called for {EventName}", eventName);
148148

149-
// Flush to ensure event is sent
150-
_telemetryClient.Flush();
151-
_logger.LogWarning("📊 Flush called for {EventName}", eventName);
149+
// Note: Don't call Flush() - SDK v3 has a bug where it throws NullReferenceException
150+
// The SDK will batch and send telemetry automatically
152151

153-
// For CPU-intensive operations, wait to ensure transmission completes
152+
// For CPU-intensive operations, wait briefly to allow the SDK to send
154153
// before background threads saturate all cores
155154
if (waitForTransmission)
156155
{
157156
_logger.LogWarning("📊 Waiting 1s for telemetry transmission...");
158-
Thread.Sleep(1000); // Give network I/O time to complete
157+
Thread.Sleep(1000);
159158
}
160159

161160
_logger.LogWarning("📊 Successfully tracked event {EventName}", eventName);

0 commit comments

Comments
 (0)