Skip to content

Commit 6d9e344

Browse files
committed
added logging to simulation id
1 parent b04cc58 commit 6d9e344

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/PerfProblemSimulator/Services/SimulationContext.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using Microsoft.ApplicationInsights;
3+
using Microsoft.Extensions.Logging;
34

45
namespace PerfProblemSimulator.Services;
56

@@ -37,14 +38,27 @@ public class SimulationContext : ISimulationContext
3738
private static readonly AsyncLocal<Guid?> _currentSimulationId = new();
3839
private static readonly AsyncLocal<string?> _currentSimulationType = new();
3940
private readonly TelemetryClient? _telemetryClient;
41+
private readonly ILogger<SimulationContext> _logger;
4042

4143
/// <summary>
4244
/// Initializes a new instance of the <see cref="SimulationContext"/> class.
4345
/// </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)
4649
{
50+
_logger = logger;
4751
_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+
}
4862
}
4963

5064
/// <inheritdoc />
@@ -81,15 +95,35 @@ public IDisposable SetContext(Guid simulationId, string simulationType)
8195
/// </summary>
8296
internal void TrackSimulationEvent(string eventName, Guid simulationId, string simulationType)
8397
{
84-
if (_telemetryClient == null) return;
98+
_logger.LogInformation(
99+
"Tracking App Insights event: {EventName} for simulation {SimulationId} ({SimulationType})",
100+
eventName, simulationId, simulationType);
85101

86-
var properties = new Dictionary<string, string>
102+
if (_telemetryClient == null)
87103
{
88-
["SimulationId"] = simulationId.ToString(),
89-
["SimulationType"] = simulationType
90-
};
104+
_logger.LogDebug("TelemetryClient is null, skipping event tracking");
105+
return;
106+
}
91107

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+
}
93127
}
94128

95129
private class ContextScope : IDisposable

0 commit comments

Comments
 (0)