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);