Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit acf3090

Browse files
Use TelemetryClient instead of SysDiag.Trace to trace AI initialization (#319)
1 parent a617770 commit acf3090

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/NuGet.Services.Logging/ApplicationInsights.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Diagnostics;
5+
using Microsoft.ApplicationInsights;
6+
using Microsoft.ApplicationInsights.DataContracts;
67
using Microsoft.ApplicationInsights.Extensibility;
78
using Microsoft.ApplicationInsights.Extensibility.Implementation;
89
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
@@ -32,21 +33,28 @@ private static void InitializeTelemetryConfiguration(string instrumentationKey,
3233
TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
3334
TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryContextInitializer());
3435

36+
// Construct a TelemetryClient to emit traces so we can track and debug AI initialization.
37+
var telemetryClient = new TelemetryClient();
38+
3539
// Configure heartbeat interval if specified.
3640
// When not defined or null, the DiagnosticsTelemetryModule will use its internal defaults (heartbeat enabled, interval of 15 minutes).
3741
if (heartbeatInterval.HasValue)
3842
{
39-
var heartbeatManager = GetHeartbeatPropertyManager();
43+
var heartbeatManager = GetHeartbeatPropertyManager(telemetryClient);
4044
if (heartbeatManager != null)
4145
{
4246
heartbeatManager.HeartbeatInterval = heartbeatInterval.Value;
4347

44-
Trace.TraceInformation($"Telemetry initialized using configured heartbeat interval: {heartbeatInterval.Value}.");
48+
telemetryClient.TrackTrace(
49+
$"Telemetry initialized using configured heartbeat interval: {heartbeatInterval.Value}.",
50+
SeverityLevel.Information);
4551
}
4652
}
4753
else
4854
{
49-
Trace.TraceInformation($"Telemetry initialized using default heartbeat interval.");
55+
telemetryClient.TrackTrace(
56+
"Telemetry initialized using default heartbeat interval.",
57+
SeverityLevel.Information);
5058
}
5159

5260
Initialized = true;
@@ -57,7 +65,7 @@ private static void InitializeTelemetryConfiguration(string instrumentationKey,
5765
}
5866
}
5967

60-
private static IHeartbeatPropertyManager GetHeartbeatPropertyManager()
68+
private static IHeartbeatPropertyManager GetHeartbeatPropertyManager(TelemetryClient telemetryClient)
6169
{
6270
if (HeartbeatManager == null)
6371
{
@@ -76,13 +84,15 @@ private static IHeartbeatPropertyManager GetHeartbeatPropertyManager()
7684
catch (Exception hearbeatManagerAccessException)
7785
{
7886
// An non-critical, unexpected exception occurred trying to access the heartbeat manager.
79-
Trace.TraceError($"There was an error accessing heartbeat manager. Details: {hearbeatManagerAccessException.ToInvariantString()}");
87+
telemetryClient.TrackTrace(
88+
$"There was an error accessing heartbeat manager. Details: {hearbeatManagerAccessException.ToInvariantString()}",
89+
SeverityLevel.Error);
8090
}
8191

8292
if (HeartbeatManager == null)
8393
{
8494
// Heartbeat manager unavailable: log warning.
85-
Trace.TraceWarning("Heartbeat manager unavailable");
95+
telemetryClient.TrackTrace("Heartbeat manager unavailable", SeverityLevel.Warning);
8696
}
8797
}
8898

0 commit comments

Comments
 (0)