You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

传入instrumentationKey时,如何禁用AddApplicationInsightsTelemetryWorkerService自适应采样

问题描述

我当前在Startup类中的实现如下:

services.AddApplicationInsightsTelemetryWorkerService(<MyAppInsightsInstrumentationKey>);

已知禁用自适应采样需要向services.AddApplicationInsightsTelemetryWorkerService()传入ApplicationInsightsServiceOptions,官方示例代码如下:

using Microsoft.ApplicationInsights.WorkerService;

public void ConfigureServices(IServiceCollection services)
{
    var aiOptions = new ApplicationInsightsServiceOptions();
    // 禁用自适应采样
    aiOptions.EnableAdaptiveSampling = false;

    // 禁用QuickPulse(实时指标流)
    aiOptions.EnableQuickPulseMetricStream = false;
    services.AddApplicationInsightsTelemetryWorkerService(aiOptions);
}

由于我已经传入了instrumentationKey,请问在此场景下禁用自适应采样的正确方式是什么?

解决方案

你只需要在创建ApplicationInsightsServiceOptions实例时,同时指定你的InstrumentationKey并禁用自适应采样即可,无需再单独传入InstrumentationKey参数,具体实现代码如下:

using Microsoft.ApplicationInsights.WorkerService;

public void ConfigureServices(IServiceCollection services)
{
    var aiOptions = new ApplicationInsightsServiceOptions
    {
        // 设置你的InstrumentationKey
        InstrumentationKey = "<MyAppInsightsInstrumentationKey>",
        // 禁用自适应采样
        EnableAdaptiveSampling = false,
        // 若不需要实时指标流,可一并禁用(可选)
        EnableQuickPulseMetricStream = false
    };

    services.AddApplicationInsightsTelemetryWorkerService(aiOptions);
}

说明

  • ApplicationInsightsServiceOptions类包含了所有Application Insights Worker Service的配置项,包括InstrumentationKey、采样设置、指标流开关等,通过该对象统一配置是更规范的方式。
  • 直接传入InstrumentationKey的重载方法本质上也是内部创建了一个默认的ApplicationInsightsServiceOptions实例并设置InstrumentationKey,因此改用Options对象配置可以覆盖默认行为,实现禁用自适应采样的需求。

内容的提问来源于stack exchange,提问作者Vanderwood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:45:55