2

I have an older console application which I'm trying to switch the tracing to use Application Insights using ApplicationInsightsTraceListener.

I'm having trouble getting the tail end of the logs without adding an arbitrary sleep to the end.

I've recreated with a very simple Console app that looks like this:

    static void Main(string[] args)
    {
        Trace.TraceInformation("Hello World!");
        Thread.Sleep(10000);
    }

With the Sleep I can see it and without I cannot.

I've tried several different variations instead of the sleep such as

        System.Diagnostics.Trace.Flush();

and

        Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryChannel.Flush();

I'm using the out of the box ApplicationInsights.config (except for my Instrumentation Key.)

This is the packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.ApplicationInsights" version="2.8.1" targetFramework="net472" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net472" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.8.1" targetFramework="net472" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.8.1" targetFramework="net472" />
  <package id="Microsoft.ApplicationInsights.TraceListener" version="2.8.1" targetFramework="net472" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.8.1" targetFramework="net472" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.8.1" targetFramework="net472" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.5.0" targetFramework="net472" />
</packages>

Update

I've improved the reliability of my delivery by ending with a block of code like this but I still don't like the sleep. For that matter, I don't like that I need to modify the code for a new trace listener but I doubt I'm getting out of that.

var channel = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryChannel as ServerTelemetryChannel;
channel.MaxTelemetryBufferDelay = TimeSpan.FromSeconds(.5);
Thread.Sleep(channel.MaxTelemetryBufferDelay);
drs9222
  • 4,448
  • 3
  • 33
  • 41
  • The only thing that worked for us is adding a `Thread.Sleep(45000);` at the end of the program. Not a great solution but it fixed all our console apps. – Lavamantis Jan 26 '21 at 20:57

1 Answers1

3

Unfortunately, there is no good solution right now. The ask for Flush which returns only after all data is persisted - is in backlog.

One option is to replace telemetry channel with InMemoryChannel. Then Flush will guarantee that data is sent to endpoint. But this channel doesn't retry if endpoint is unavailable, so there is still a potential for losing data.

Another option is come up with own telemetry channel. But that might require lots of work to make it reliable.

ServerTelemetryChannel will resend data when console app is back. Not sure whether it helps with your scenario though.

ZakiMa
  • 5,637
  • 1
  • 24
  • 48
  • Thanks. That's about what I had found although in practice I've never gotten ServerTelemetryChannel to resend data when the console app is back. – drs9222 Dec 14 '18 at 12:40
  • One option - open a new issue on github for not resending data for console app. Team will investigate. – ZakiMa Dec 14 '18 at 17:34