首页 > 解决方案 > 在 Azure 应用程序见解中查找“应用程序停止事件”

问题描述

我有一个有时会经历“平台(基础设施升级)”事件的 Web 应用程序。

我可以检测到这些的唯一方法是转到 Azure 门户并向下钻取

并寻找如下错误

在此处输入图像描述

我的问题是,我是否可以使用 Application Insights 查询 Azure 以了解这些事件何时发生?

看似简单的事情,却想不通。

标签: azureloggingazure-application-insights

解决方案


我可以使用 Application Insights 查询 Azure 以了解这些事件何时发生吗?

不,但是您的应用程序可能能够在启动或关闭时记录事件?例如,在 .Net Core 应用程序中,您可以像这样监听停止/启动事件:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime, TelemetryClient telemetryClient)
{
    hostApplicationLifetime.ApplicationStarted.Register(() => { telemetryClient.TrackEvent("App Started"); });
    hostApplicationLifetime.ApplicationStopping.Register(() => { telemetryClient.TrackEvent("App Stopping"); });
    hostApplicationLifetime.ApplicationStopped.Register(() => {
        telemetryClient.TrackEvent("App Stopped");
        telemetryClient.Flush();

        Thread.Sleep(TimeSpan.FromSeconds(5));
    });

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();
}

它使用IHostApplicationLifetime接口来获取应用程序生命周期事件的通知。


推荐阅读