Skip to content

Commit

Permalink
NLogTarget - Use isolated TelemetryClient instead of modifying global…
Browse files Browse the repository at this point in the history
… config
  • Loading branch information
snakefoot committed Aug 13, 2024
1 parent e4a0e8b commit e623c1b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,22 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
protected override void InitializeTarget()
{
base.InitializeTarget();
#pragma warning disable CS0618 // Type or member is obsolete: TelemtryConfiguration.Active is used in TelemetryClient constructor.
this.telemetryClient = new TelemetryClient();
#pragma warning restore CS0618 // Type or member is obsolete

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

// Check if nlog application insights target has connectionstring in config file then
// configure telemetryclient with the connectionstring otherwise using instrumentationkey.
// configure new telemetryclient with the connectionstring otherwise using legacy instrumentationkey.
if (!string.IsNullOrWhiteSpace(connectionString))
{
this.telemetryClient.TelemetryConfiguration.ConnectionString = connectionString;
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = connectionString;
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete: TelemtryConfiguration.Active is used in TelemetryClient constructor.
this.telemetryClient = new TelemetryClient();
#pragma warning restore CS0618 // Type or member is obsolete
string instrumentationKey = this.instrumentationKeyLayout.Render(LogEventInfo.CreateNullEvent());
if (!string.IsNullOrWhiteSpace(instrumentationKey))
{
Expand Down

0 comments on commit e623c1b

Please sign in to comment.