5

For Flush() method in Azure App Insights, I was wondering if it impacts the performance of the project?

I tried to remove Flush() and all the custom data are still sent to App Insights.So my question should be why do we need the Flush()? Can we remove it?

superninja
  • 3,114
  • 7
  • 30
  • 63
  • See docs - https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#flushing-data – Rory Jan 16 '22 at 10:44

1 Answers1

10

Flush() on TelemetryClient pushes all the data it currently has in a buffer to the App Insights service. You can see its source code here: https://github.com/Microsoft/ApplicationInsights-dotnet/blob/3115fe1cc866a15d09e9b5f1f7f596385406433d/src/Microsoft.ApplicationInsights/TelemetryClient.cs#L593.

Normally, Application Insights will send your data in batches in the background so it uses the network more efficiently. If you have developer mode enabled or call Flush() manually, data is sent immediately.

Typically you do not need to call Flush(). But in a case where you know the process will exit after that point, you'll want to call Flush() to make sure all the data is sent.

juunas
  • 54,244
  • 13
  • 113
  • 149
  • 3
    If the application is exiting, then a Sleep(5000) is recommended after Flush() to ensure all items are sent. – cijothomas Aug 21 '18 at 18:59
  • 3
    Hmm that doesn't look clean at all. We should be able to explicitly await something, maybe that exit code is critical telemetry. – evilSnobu Aug 22 '18 at 06:31
  • @evilSnobu make sense, any sample code you could provide? It's a web api for asp.net core. I would assume the controller class would be the exit code. – superninja Aug 22 '18 at 16:44
  • 2
    I found some useful sample code: https://thinkrethink.net/2017/03/09/application-shutdown-in-asp-net-core/ – superninja Aug 22 '18 at 21:09