Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Version 2.3.0-beta2
- [Fixed race condition on dispose to close #651](https://github.com/Microsoft/ApplicationInsights-aspnetcore/pull/652)
-Removed DomainNameRoleInstanceTelemetryInitializer as it is deprecated.
-Reuse AzureWebAppRoleEnvironmentTelemetryInitializer from WindowsServer repo instead of outdated implementation in this repo.
- Updated Web/Base SDK version dependency to 2.6.0-beta4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ namespace Microsoft.ApplicationInsights.AspNetCore
{
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using Extensions;
using Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading;

/// <summary>
/// Class used to initialize Application Insight diagnostic listeners.
/// </summary>
internal class ApplicationInsightsInitializer : IObserver<DiagnosticListener>, IDisposable
{
private readonly List<IDisposable> subscriptions;
private ConcurrentBag<IDisposable> subscriptions;
private readonly IEnumerable<IApplicationInsightDiagnosticListener> diagnosticListeners;

/// <summary>
Expand All @@ -26,7 +28,7 @@ public ApplicationInsightsInitializer(
IServiceProvider serviceProvider)
{
this.diagnosticListeners = diagnosticListeners;
this.subscriptions = new List<IDisposable>();
this.subscriptions = new ConcurrentBag<IDisposable>();

// Add default logger factory for debug mode only if enabled and instrumentation key not set
if (options.Value.EnableDebugLogger && string.IsNullOrEmpty(options.Value.InstrumentationKey))
Expand All @@ -48,11 +50,17 @@ public void Start()
/// <inheritdoc />
void IObserver<DiagnosticListener>.OnNext(DiagnosticListener value)
{
var subs = Volatile.Read(ref this.subscriptions);
if (subs is null)
{
return;
}

foreach (var applicationInsightDiagnosticListener in this.diagnosticListeners)
{
if (applicationInsightDiagnosticListener.ListenerName == value.Name)
{
this.subscriptions.Add(value.SubscribeWithAdapter(applicationInsightDiagnosticListener));
subs.Add(value.SubscribeWithAdapter(applicationInsightDiagnosticListener));
}
}
}
Expand All @@ -75,13 +83,21 @@ public void Dispose()

protected virtual void Dispose(bool disposing)
{
if (disposing)
if (!disposing)
{
foreach (var subscription in this.subscriptions)
{
subscription.Dispose();
}
return;
}

var subs = Interlocked.Exchange(ref this.subscriptions, null);
if (subs is null)
{
return;
}

foreach (var subscription in subs)
{
subscription.Dispose();
}
}
}
}
}