Bart
Bart

Reputation: 231

ApplicationInsights, ApplicationInsightsTelemetryWorkerService, Set EnableAdaptiveSampling in config

In a "worker" application (.net core 3.1) I have configured an ApplicationInsightsTelemetryWorkerService by:

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

In the appsettings.json the InstrumentationKey is defined as:

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

I want to move the "EnableAdaptiveSampling" setting from the code to json file. When I try to add it to the ApplicationInsights section like "EnableAdaptiveSampling": false, it doesn't seem to pick it up.

What is the correct way to set this in a config file

Upvotes: 0

Views: 1025

Answers (1)

krishg
krishg

Reputation: 6508

You have to read the configuration by code and set it. In .net core, there is no automatic pickup of predefined setting for adaptive sampling like InstrumentationKey. For example:

appsettings.json:

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

Code:

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

Upvotes: 2

Related Questions