Skip to content
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

ApplicationInsight.Flush() #13

Open
ranouf opened this issue Oct 1, 2019 · 2 comments
Open

ApplicationInsight.Flush() #13

ranouf opened this issue Oct 1, 2019 · 2 comments

Comments

@ranouf
Copy link

ranouf commented Oct 1, 2019

Hi,

I created a LoggerService :

public class LoggerService : ILoggerService where T : class
{
private readonly TelemetryClient _telemetryClient;
private readonly ILogger _logger;

    public LoggerService(
        TelemetryClient telemetryClient,
        ILogger<T> logger
    )
    {
        _telemetryClient = telemetryClient;
        _logger = logger;
    }
    public void Flush()
    {
        _telemetryClient.Flush();
    }

    public void Dispose()
    {
        Flush();
    }
}

And BlockingDetector log this Warning:

2019-10-01 21:02:39.607 +00:00 [Warning] Ben.Diagnostics.BlockingMonitor: Blocking method has been invoked and blocked, this can lead to threadpool starvation.
at System.Threading.Tasks.TplEtwProvider.TaskWaitBegin(Int32 OriginatingTaskSchedulerID, Int32 OriginatingTaskID, Int32 TaskID, TaskWaitBehavior Behavior, Int32 ContinueWithTaskID)at System.Threading.Tasks.Task.InternalWaitCore(Int32 millisecondsTimeout, CancellationToken cancellationToken)at Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel.Flush()at Microsoft.ApplicationInsights.TelemetryClient.Flush()at SK.Common.Logging.LoggerService`1.Dispose()

I understand, Flush should be FlushAsync(microsoft/ApplicationInsights-dotnet#482), many asked for this, but anyway, as Dispose is Synchrone, I have no other way to do it.

So maybe some people already notice it and find a correct way to flush the telemetry client?

But if not, how can I filter this issue in BlockingDetector?

@benaadams
Copy link
Owner

ServerTelemetryChannel.Flush does block as it calls TelemetryBuffer.FlushAsync().GetAwaiter().GetResult() which is an issue with ApplicationInsights as it requires 2 threads to complete as highlighted in @davidfowl's AsyncGuidance which is being moved the official docs dotnet/AspNetCore.Docs#14734. So yes it should offer a FlushAsync method.

but anyway, as Dispose is Synchrone, I have no other way to do it.

Not sure what the interface ILoggerService is, however if you are using .NET Core 3.0 you can implement IAsyncDisposable and also use it with C#8 in an async using statement; however the docs haven't been reflected to update that either yet dotnet/docs#11984

@ranouf
Copy link
Author

ranouf commented Oct 2, 2019

Thanks for this complete and up to date answer!

I found IAsyncDisposable available in :

In my LoggerService, I now have:

    public async Task DisposeAsync()
    {
        await Task.Run(() => _telemetryClient.Flush());
    }

And from what I can see, no warning anymore :).

Note: So my logger service is a service wrapping TelemetryClient and ILogger, it helps to faster log informations in our app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants