我有一个旧的控制台应用程序,我正在尝试使用ApplicationInsightsTraceListener将跟踪切换为使用ApplicationInsiights。
我很难在不添加任意睡眠的情况下获取日志的尾部。
我用一个非常简单的控制台应用程序重新创建了它,看起来像这样:
static void Main(string[] args)
{
Trace.TraceInformation("Hello World!");
Thread.Sleep(10000);
}
有了Sleep
,我可以看到它,没有它我就看不到。
我已经尝试了几种不同的变化而不是睡眠,比如
System.Diagnostics.Trace.Flush();
和
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryChannel.Flush();
我正在使用开箱即用的ApplicationInsights.config(除了我的Instrumentation Key。(
这是软件包。配置:
<?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>
更新
我以这样的代码块结束,提高了交付的可靠性,但我仍然不喜欢睡眠。就这一点而言,我不喜欢为新的跟踪侦听器修改代码,但我怀疑我是否能摆脱这种情况。
var channel = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryChannel as ServerTelemetryChannel;
channel.MaxTelemetryBufferDelay = TimeSpan.FromSeconds(.5);
Thread.Sleep(channel.MaxTelemetryBufferDelay);
不幸的是,目前还没有好的解决方案。只有在所有数据都被持久化后才会返回的请求Flush在backlog中。
一种选择是用InMemoryChannel替换遥测通道。然后Flush将保证数据被发送到端点。但如果端点不可用,该通道不会重试,因此仍有可能丢失数据。
另一种选择是建立自己的遥测通道。但这可能需要大量的工作才能使其可靠。
当控制台应用程序返回时,ServerTelemetryChannel将重新发送数据。但不确定这是否有助于你的场景。