TelemetryClient.Flush() 在应用程序退出期间需要大约 2 分钟到 运行

TelemetryClient.Flush() takes about 2 minutes to run during application exit

我有一个 .NET Core 3.1 控制台应用程序 运行 作为托管服务。当应用程序关闭时,它会挂起大约 2 分钟。闯入时我可以看到它挂在 TelemetryClient.Flush() 上,特别是在 InMemoryChannel.Flush().

所以我在 Fiddler 中查看结果,我可以看到应用程序正在向 https://dc.services.visualstudio.com/v2/track 发出请求以报告遥测数据,但该服务没有响应。最终在 2 分钟后,响应失败并显示 500,并且在 Web 响应中:"IIS 10.0 Detailed Error - 500.1013 - Internal Server Error".

我不知道这是不是我做错了什么。所以我将应用程序缩短到最低限度。

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace DotNetCoreConsoleAppTest
{
    public class Program
    {
        public static async Task Main()
        {
            await Host.CreateDefaultBuilder()
                .ConfigureServices(services =>
                {
                    services
                        .Configure<TelemetryConfiguration>(options =>
                        {
                            options.InstrumentationKey = "<put your key here>";
                        })
                        .AddSingleton<TelemetryWriter>()
                        .AddHostedService<ProgramHost>();
                })
                .RunConsoleAsync()
                .ConfigureAwait(false);
        }
    }

    public class TelemetryWriter : IDisposable
    {
        private readonly TelemetryClient _telemetryClient;

        public TelemetryWriter(IOptions<TelemetryConfiguration> telemetryConfiguration)
        {
            _telemetryClient = new TelemetryClient(telemetryConfiguration.Value);
        }

        public void WriteEvent() => _telemetryClient.TrackEvent("test");

        public void Dispose() => _telemetryClient.Flush();
    }

    public class ProgramHost : IHostedService
    {
        private readonly TelemetryWriter _telemetryWriter;
        private readonly IHostApplicationLifetime _hostApplicationLifetime;

        public ProgramHost(
            TelemetryWriter telemetryWriter,
            IHostApplicationLifetime hostApplicationLifetime)
        {
            _telemetryWriter = telemetryWriter;
            _hostApplicationLifetime = hostApplicationLifetime;
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            _telemetryWriter.WriteEvent();
            _hostApplicationLifetime.StopApplication();
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
    }
}

部分内容来自网络回复:

<div class="content-container"> 
 <fieldset><h4>Detailed Error Information:</h4> 
  <div id="details-left"> 
   <table border="0" cellpadding="0" cellspacing="0"> 
    <tr class="alt"><th>Module</th><td>&nbsp;&nbsp;&nbsp;iisnode</td></tr> 
    <tr><th>Notification</th><td>&nbsp;&nbsp;&nbsp;ExecuteRequestHandler</td></tr> 
    <tr class="alt"><th>Handler</th><td>&nbsp;&nbsp;&nbsp;iisnode</td></tr> 
    <tr><th>Error Code</th><td>&nbsp;&nbsp;&nbsp;0x0000006d</td></tr> 

   </table> 
  </div> 
  <div id="details-right"> 
   <table border="0" cellpadding="0" cellspacing="0"> 
    <tr class="alt"><th>Requested URL</th><td>&nbsp;&nbsp;&nbsp;https://dc.services.visualstudio.com:443/InputServer.js</td></tr> 
    <tr><th>Physical Path</th><td>&nbsp;&nbsp;&nbsp;E:\sitesroot[=13=]\InputServer.js</td></tr> 
    <tr class="alt"><th>Logon Method</th><td>&nbsp;&nbsp;&nbsp;Anonymous</td></tr> 
    <tr><th>Logon User</th><td>&nbsp;&nbsp;&nbsp;Anonymous</td></tr> 
    <tr class="alt"><th>Request Tracing Directory</th><td>&nbsp;&nbsp;&nbsp;C:\Resources\directory\f3eec886680f474eb56deb0e59f20036.Breeze.DiagnosticStore\FailedReqLogFiles\Web</td></tr> 
   </table> 
   <div class="clear"></div> 
  </div> 
 </fieldset> 
</div> 

挂起的堆栈跟踪是:

System.Private.CoreLib.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)    Unknown
System.Private.CoreLib.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)  Unknown
System.Private.CoreLib.dll!System.Threading.Tasks.Task.InternalWaitCore(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)  Unknown
System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task task)    Unknown
System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.GetResult()  Unknown
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Channel.InMemoryTransmitter.DequeueAndSend(System.TimeSpan timeout) Unknown
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Channel.InMemoryTransmitter.Flush(System.TimeSpan timeout)  Unknown
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Channel.InMemoryChannel.Flush(System.TimeSpan timeout)  Unknown
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Channel.InMemoryChannel.Flush() Unknown
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.TelemetryClient.Flush() Unknown
DotNetCoreConsoleAppTest.dll!DotNetCoreConsoleAppTest.TelemetryWriter.Dispose() Line 43 C#
Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.DisposeAsync()   Unknown
Microsoft.Extensions.Hosting.dll!Microsoft.Extensions.Hosting.Internal.Host.DisposeAsync()  Unknown
Microsoft.Extensions.Hosting.Abstractions.dll!Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(Microsoft.Extensions.Hosting.IHost host, System.Threading.CancellationToken token)    Unknown
Microsoft.Extensions.Hosting.dll!Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.RunConsoleAsync(Microsoft.Extensions.Hosting.IHostBuilder hostBuilder, System.Threading.CancellationToken cancellationToken) Unknown
DotNetCoreConsoleAppTest.dll!DotNetCoreConsoleAppTest.Program.Main() Line 16    C#
DotNetCoreConsoleAppTest.dll!DotNetCoreConsoleAppTest.Program.<Main>()  Unknown

尽管如此,事件确实出现在 Application Insights 的日志中。唯一的问题是我的主机应用程序挂起。这是我尝试 Flush() 的方式有问题吗?或者这是一个 Application Insights 服务问题?

ZakiMa 回答了我的问题 -- App Insights 出现故障。