首页 > 解决方案 > ApplicationInsights,ApplicationInsightsTelemetryWorkerService,在配置中设置 EnableAdaptiveSampling

问题描述

在“worker”应用程序(.net core 3.1)中,我通过以下方式配置了 ApplicationInsightsTelemetryWorkerService:

 services.AddApplicationInsightsTelemetryWorkerService(options =>
                     {
                         options.DeveloperMode = false;
                         options.EnableAdaptiveSampling = false;
                     });

在 appsettings.json 中 InstrumentationKey 定义为:

  "ApplicationInsights": {
    "InstrumentationKey": "xxxx-xxxx-xxxx-xxxx-xxxx"   
    },

我想将“EnableAdaptiveSampling”设置从代码移动到 json 文件。当我尝试将它添加到“EnableAdaptiveSampling”之类的 ApplicationInsights 部分时:false,它似乎没有捡起它。

在配置文件中设置它的正确方法是什么

标签: c#azure-application-insights

解决方案


您必须通过代码读取配置并进行设置。在 .net 核心中,没有像 InstrumentationKey 这样的自适应采样的预定义设置的自动拾取。例如:

appsettings.json:

"ApplicationInsights": {
    "InstrumentationKey": "xxxx-xxxx-xxxx-xxxx-xxxx",
    "EnableAdaptiveSampling": "false"
    },

代码:

// instrumentation key is read automatically from appsettings.json
services.AddApplicationInsightsTelemetryWorkerService(options =>
                     {
                         options.DeveloperMode = false;
                         options.EnableAdaptiveSampling = hostContext.Configuration.GetValue<bool>("ApplicationInsights:EnableAdaptiveSampling");
                     });

推荐阅读