Skip to content

Commit 38a5bcb

Browse files
committed
app insights telemetry debugging
1 parent 5d592fb commit 38a5bcb

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/PerfProblemSimulator/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@
336336

337337
// Log startup information
338338
var logger = app.Services.GetRequiredService<ILogger<Program>>();
339-
logger.LogInformation("Performance Problem Simulator starting...");
339+
logger.LogWarning(
340+
"🚀 PerfProblemSimulator starting. This message should appear in AppTraces if App Insights logging is configured correctly.");
340341
logger.LogInformation(
341342
"Problem endpoints are {Status}",
342343
Environment.GetEnvironmentVariable("DISABLE_PROBLEM_ENDPOINTS")?.Equals("true", StringComparison.OrdinalIgnoreCase) == true

src/PerfProblemSimulator/Services/SimulationContext.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics;
22
using Microsoft.ApplicationInsights;
3-
using Microsoft.Extensions.DependencyInjection;
43
using Microsoft.Extensions.Logging;
54

65
namespace 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

src/PerfProblemSimulator/appsettings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"LogLevel": {
44
"Default": "Information",
55
"Microsoft.AspNetCore": "Warning",
6-
"PerfProblemSimulator": "Information"
6+
"PerfProblemSimulator": "Warning"
7+
},
8+
"ApplicationInsights": {
9+
"LogLevel": {
10+
"Default": "Warning",
11+
"PerfProblemSimulator": "Warning"
12+
}
713
}
814
},
915
"AllowedHosts": "*",

0 commit comments

Comments
 (0)