【发布时间】:2018-12-13 18:31:36
【问题描述】:
我有一个较旧的控制台应用程序,我正在尝试使用 ApplicationInsightsTraceListener 切换跟踪以使用 Application Insights。
如果没有在结尾添加任意睡眠,我无法获取日志的结尾。
我重新创建了一个非常简单的控制台应用程序,如下所示:
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 除外。)
这是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>
更新
通过以这样的代码块结尾,我提高了交付的可靠性,但我仍然不喜欢睡觉。就此而言,我不喜欢我需要为新的跟踪侦听器修改代码,但我怀疑我是否能摆脱这种情况。
var channel = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryChannel as ServerTelemetryChannel;
channel.MaxTelemetryBufferDelay = TimeSpan.FromSeconds(.5);
Thread.Sleep(channel.MaxTelemetryBufferDelay);
【问题讨论】:
-
唯一对我们有用的是在程序末尾添加
Thread.Sleep(45000);。不是一个很好的解决方案,但它修复了我们所有的控制台应用程序。
标签: c# .net azure console-application azure-application-insights