Skip to content

Implement IAsyncDisposable and use FlushAsync on TelemetryClient #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Serilog.Core;
Expand All @@ -29,6 +30,9 @@ namespace Serilog.Sinks.ApplicationInsights;
/// Inspired by their NLog Appender implementation.
/// </summary>
public class ApplicationInsightsSink : ILogEventSink, IDisposable
#if NET6_0_OR_GREATER
, IAsyncDisposable
#endif
{
readonly IFormatProvider _formatProvider;

Expand Down Expand Up @@ -191,6 +195,29 @@ protected virtual void Dispose(bool disposeManagedResources)
IsDisposing = false;
}
}

#if NET6_0_OR_GREATER
/// <summary>
/// Disposes the sink and flushes telemetry to App Insights.
/// </summary>
public async ValueTask DisposeAsync()
{
if (IsDisposing || IsDisposed)
return;

try
{
IsDisposing = true;
if (_telemetryClient is not null) await _telemetryClient.FlushAsync(CancellationToken.None);
}
finally
{
_telemetryClient = null;
IsDisposed = true;
IsDisposing = false;
}
}
#endif

#endregion Implementation of IDisposable
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected ApplicationInsightsTest(ITelemetryConverter converter = null)
}

protected ILogger Logger { get; }
protected UnitTestTelemetryChannel Channel => _channel;

protected List<ITelemetry> SubmittedTelemetry => _channel.SubmittedTelemetry;

Expand Down
24 changes: 24 additions & 0 deletions test/Serilog.Sinks.ApplicationInsights.Tests/DisposableTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace Serilog.Sinks.ApplicationInsights.Tests;

public class DisposableTest : ApplicationInsightsTest
{
[Fact]
public void Flush_is_called_on_channel_when_logger_is_disposed()
{
((IDisposable)Logger).Dispose();
Assert.True(Channel.FlushCalled, "Channel.Flush was not called");
}

#if NET6_0_OR_GREATER
[Fact]
public async Task FlushAsync_is_called_on_channel_when_logger_is_disposed_asynchronously()
{
await ((IAsyncDisposable)Logger).DisposeAsync();
Assert.True(Channel.FlushAsyncCalled, "Channel.FlushAsync was not called");
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.Channel;

namespace Serilog.Sinks.ApplicationInsights.Tests;

class UnitTestTelemetryChannel : ITelemetryChannel
public class UnitTestTelemetryChannel : ITelemetryChannel, IAsyncFlushable
{
public List<ITelemetry> SubmittedTelemetry { get; } = new();
public bool FlushCalled { get; private set; }
public bool FlushAsyncCalled { get; private set; }

public bool? DeveloperMode
{
Expand All @@ -25,6 +29,13 @@ public void Dispose()

public void Flush()
{
FlushCalled = true;
}

public Task<bool> FlushAsync(CancellationToken cancellationToken)
{
FlushAsyncCalled = true;
return Task.FromResult(true);
}

public void Send(ITelemetry item)
Expand Down
Loading