TelemetryClient does not send any data unless Flush is called

8.3k views Asked by At

I'm using TelemetryClient (v0.17.0.576) directly in my code and it looks like I can push data to Azure only when I manually call Flush at the end which feels wrong. Am I missing something here ?

var configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = "KEY";
var client = new TelemetryClient(configuration);

for (int i = 0; i < 10; i++)
{
   log.Information("Loop: {0} {1}", i, value);

   client.Track(new TraceTelemetry(value));
}
client.Flush();
4

There are 4 answers

2
Mario Hewardt On BEST ANSWER

For performance reasons, the Application Insights SDK batches telemetry and sends it up in chunks. To see this in action you can replace your Flush call with a call to Thread.Sleep (70000) and you will see the instrumentation uploaded to AI once the app terminates.

0
Eike On

The data does not need to be flushed - except when the application ends directly after sending the last trace. Unfortunately, Flush is not enough though, it doesn't block(*). So the recommendation is to do both flushing and five seconds of waiting.

(*) https://github.com/microsoft/ApplicationInsights-dotnet/issues/407

0
V Yadav On

Thread.sleep(30000) is working for me, but flush is not working in application. I would like to use Flush() method as i can't sleep my application for 30 seconds. Is Flush() method is related to version Specific or i am missing something else?

I have Console application which call another Console Library, which will further call another console library which have telemetry class (ie Console Application --> DLL --> DLL (having telemetry implementation)).

2
James Bateson On

Adding to Mario Hewardt's answer. If you use the persistence channel:

TelemetryConfiguration.Active.TelemetryChannel = new PersistenceChannel();

Flush() is synchronous (so you don't need sleep the thread for an abitrary length of time). It also has the benefit of saving the telemetry data to a local file if Application Insights cannot be contacted, which will then be sent next time Flush() is called with a good connection.