Skip to content
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
3 changes: 3 additions & 0 deletions LOGGING/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Version 2.12.0-beta1
- [ILogger - Flush TelemetryChannel when the ILoggerProvider is Disposed.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1289)

## Version 2.11.0
- Update Base SDK to 2.11.0
- Update System.Diagnostics.DiagnosticSource to 4.6.0.
Expand Down
7 changes: 6 additions & 1 deletion LOGGING/src/ILogger/ApplicationInsightsLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Microsoft.Extensions.Logging.ApplicationInsights
{
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.Logging;

/// <summary>
/// <see cref="ApplicationInsightsLoggerOptions"/> defines the custom behavior of the tracing information sent to Application Insights.
Expand All @@ -26,5 +25,11 @@ public class ApplicationInsightsLoggerOptions
/// Defaults to true.
/// </summary>
public bool IncludeScopes { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether to flush telemetry when disposing
/// of the logger provider.
/// </summary>
public bool FlushOnDispose { get; set; } = true;
}
}
10 changes: 8 additions & 2 deletions LOGGING/src/ILogger/ApplicationInsightsLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Microsoft.Extensions.Logging.ApplicationInsights
{
using System;
using System.Collections.Concurrent;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
Expand Down Expand Up @@ -105,7 +104,14 @@ public void SetScopeProvider(IExternalScopeProvider externalScopeProvider)
/// <param name="releasedManagedResources">Release managed resources.</param>
protected virtual void Dispose(bool releasedManagedResources)
{
// Nothing to dispose right now.
if (releasedManagedResources && this.applicationInsightsLoggerOptions.FlushOnDispose)
{
this.telemetryClient.Flush();

// With the ServerTelemetryChannel, Flush pushes buffered telemetry to the Transmitter,
// but it doesn't guarantee that all events have been transmitted to the endpoint.
// TODO: Should we sleep here? Should that be controlled by options?
}
}
}
}
37 changes: 35 additions & 2 deletions LOGGING/test/ILogger.NetStandard.Tests/ILoggerIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,39 @@ public void DefaultLoggerOptionsAreCorrectlyRegistered()
Assert.IsTrue(registeredOptions.Value.IncludeScopes);
}

[TestMethod]
[TestCategory("ILogger")]
public void TelemetryChannelIsFlushedWhenServiceProviderIsDisposed()
{
TestTelemetryChannel testTelemetryChannel = new TestTelemetryChannel();

using (ServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
delegate { },
telemetryConfiguration => telemetryConfiguration.TelemetryChannel = testTelemetryChannel))
{
serviceProvider.GetRequiredService<ILogger<ILoggerIntegrationTests>>();
}

Assert.AreEqual(1, testTelemetryChannel.FlushCount);
}

[TestMethod]
[TestCategory("ILogger")]
public void TelemetryChannelIsNotFlushedWhenFlushOnDisposeIsFalse()
{
TestTelemetryChannel testTelemetryChannel = new TestTelemetryChannel();

using (ServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
delegate { },
telemetryConfiguration => telemetryConfiguration.TelemetryChannel = testTelemetryChannel,
applicationInsightsOptions => applicationInsightsOptions.FlushOnDispose = false))
{
serviceProvider.GetRequiredService<ILogger<ILoggerIntegrationTests>>();
}

Assert.AreEqual(0, testTelemetryChannel.FlushCount);
}

/// <summary>
/// Sets up the Application insights logger.
/// </summary>
Expand All @@ -256,7 +289,7 @@ public void DefaultLoggerOptionsAreCorrectlyRegistered()
/// <param name="configureApplicationInsightsOptions">Action to configure logger options.</param>
/// <param name="configureServices">Action to add, configure services to DI container.</param>
/// <returns>Built DI container.</returns>
private static IServiceProvider SetupApplicationInsightsLoggerIntegration(
private static ServiceProvider SetupApplicationInsightsLoggerIntegration(
Action<ITelemetry, ITelemetryProcessor> telemetryActionCallback,
Action<TelemetryConfiguration> configureTelemetryConfiguration = null,
Action<ApplicationInsightsLoggerOptions> configureApplicationInsightsOptions = null,
Expand Down Expand Up @@ -298,7 +331,7 @@ private static IServiceProvider SetupApplicationInsightsLoggerIntegration(
services = configureServices.Invoke(services);
}

IServiceProvider serviceProvider = services.BuildServiceProvider();
ServiceProvider serviceProvider = services.BuildServiceProvider();

return serviceProvider;
}
Expand Down
30 changes: 30 additions & 0 deletions LOGGING/test/ILogger.NetStandard.Tests/TestTelemetryChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// <copyright file="ILoggerIntegrationTests.cs" company="Microsoft">
// Copyright © Microsoft. All Rights Reserved.
// </copyright>

using Microsoft.ApplicationInsights.Channel;

namespace Microsoft.ApplicationInsights
{
internal sealed class TestTelemetryChannel : ITelemetryChannel
{
public TestTelemetryChannel()
{
}

public bool? DeveloperMode { get; set; }
public string EndpointAddress { get; set; }

public void Dispose()
{
}

public void Flush() => FlushCount++;

public void Send(ITelemetry item) => SendCount++;

public int FlushCount { get; private set; }

public int SendCount { get; private set; }
}
}