Skip to content

Commit e623c1b

Browse files
committed
NLogTarget - Use isolated TelemetryClient instead of modifying global config
1 parent e4a0e8b commit e623c1b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,22 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
125125
protected override void InitializeTarget()
126126
{
127127
base.InitializeTarget();
128-
#pragma warning disable CS0618 // Type or member is obsolete: TelemtryConfiguration.Active is used in TelemetryClient constructor.
129-
this.telemetryClient = new TelemetryClient();
130-
#pragma warning restore CS0618 // Type or member is obsolete
131128

132129
string connectionString = this.connectionStringLayout.Render(LogEventInfo.CreateNullEvent());
133130

134131
// Check if nlog application insights target has connectionstring in config file then
135-
// configure telemetryclient with the connectionstring otherwise using instrumentationkey.
132+
// configure new telemetryclient with the connectionstring otherwise using legacy instrumentationkey.
136133
if (!string.IsNullOrWhiteSpace(connectionString))
137134
{
138-
this.telemetryClient.TelemetryConfiguration.ConnectionString = connectionString;
135+
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
136+
telemetryConfiguration.ConnectionString = connectionString;
137+
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
139138
}
140139
else
141140
{
141+
#pragma warning disable CS0618 // Type or member is obsolete: TelemtryConfiguration.Active is used in TelemetryClient constructor.
142+
this.telemetryClient = new TelemetryClient();
143+
#pragma warning restore CS0618 // Type or member is obsolete
142144
string instrumentationKey = this.instrumentationKeyLayout.Render(LogEventInfo.CreateNullEvent());
143145
if (!string.IsNullOrWhiteSpace(instrumentationKey))
144146
{

0 commit comments

Comments
 (0)