Skip to content

Commit

Permalink
Merge pull request #228 from agehrke/flushasync
Browse files Browse the repository at this point in the history
Implement IAsyncDisposable and use FlushAsync on TelemetryClient
  • Loading branch information
nblumhardt authored Oct 31, 2024
2 parents eaec9c8 + b9d54d9 commit 110dde7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
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

0 comments on commit 110dde7

Please sign in to comment.