无法在已部署的 webapi 应用程序上获取实时指标流

Cannot get live metrics stream on deployed webapi application

我有一个 webapi 应用程序,配置了 Serilog 日志记录以转到 Application Insights。 当我的 webapi 应用程序在本地 运行 时,我可以通过 Application Insights 中的 "Search" 查看应用程序的日志,我还可以看到实时指标流。

然而,当部署应用程序时,我看不到实时指标流(它显示“不可用:您的应用程序处于离线状态或使用旧版 SDK”)。有趣的是,我仍然可以在搜索中看到应用程序的日志功能 - 所以应用程序自己的日志记录在工作,但不是实时指标数据收集。

来自该服务器的 AI troubleshooting documentation suggests that this is likely to be a firewall problem, however I've verified that I can reach port 443 of the live metrics stream ports(rt.services.visualstudio.com 和 rt.applicationinsights.microsoft.com)使用 telnet。

服务器上的 Application Insights Monitor 显示站点是 "enabled"。唯一的通知是 "Application Insights is already enabled for this application through the application project code. (...)"。最初我有一个关于无法收集指标的警告,但我将应用程序池的标识添加到适当的组并且该警告现在消失了。

我所做的唯一真正的定制是从 applicationinsights.config 文件中删除 AI 密钥,并在代码中设置它,这样我就可以为每个环境拥有不同的 AI 资源。

我在Startup.Configuration方法中设置检测键的代码如下:

    if (TelemetryConfiguration.Active != null)
    {
        TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["ApplicationInsightsKey"];
    }

同样,这是在本地工作,所以我认为问题不在于代码中 AI 键的设置。

是否有任何其他 "self-logging" 我可以为 AI 打开以查看为什么它在收集或发送实时指标时遇到问题?

您能否尝试以下操作并告诉我们是否有效?

  1. 从 ApplicationInsights.config
  2. 中删除 QuickPul​​seTelemetryModule
  3. 在代码中,设置检测密钥后,手动创建模块:

    QuickPulseTelemetryModule quickPulseModule = new QuickPulseTelemetryModule();
    QuickPulseTelemetryProcessor quickPulseTelemetryProcessor = TelemetryConfiguration.Active.TelemetryProcessors.OfType<QuickPulseTelemetryProcessor>().Single();
    quickPulseModule.RegisterTelemetryProcessor(quickPulseTelemetryProcessor);
    quickPulseModule.Initialize(TelemetryConfiguration.Active);
    
  4. 确保在应用程序的整个生命周期中都保留对模块的引用。

如果上述操作失败表明在配置中找不到 QuickPul​​seTelemetryProcessor,请在部署到远程服务器时检查 ApplicationInsights.config 是否正确放置(事实上,事情在本地工作但在部署时不工作可能是文件未被拾取这一事实的一个指标 - 以及 QuickPul​​seTelemetryProcessor 似乎没有在配置中找到的事实,即使它在配置文件中)。比较 ApplicationInsights.config 在本地设置中有效的位置和在远程设置中无效的位置。如果您遇到困难,可以尝试在代码中同时创建模块和处理器。为此,从 ApplicationInsights.config 中删除 QuickPul​​seTelemetryModule 和 QuickPul​​seTelemetryProcessor(即使这可能无关紧要),并在代码中执行以下操作:

var quickPulseModule = new QuickPulseTelemetryModule();                
quickPulseModule.Initialize(TelemetryConfiguration.Active);
TelemetryConfiguration.Active.TelemetryProcessorChainBuilder.Use(next =>
            {
                var processor = new QuickPulseTelemetryProcessor(next);
                quickPulseModule.RegisterTelemetryProcessor(processor);
                return processor;
            });

TelemetryConfiguration.Active.TelemetryProcessorChainBuilder.Build();

在部署您的应用程序的服务器上 - 检查 ApplicationInsights.config 的位置。根据许多因素(如部署模型或应用程序类型),该文件应位于 /bin 文件夹中或应用程序的根目录中。