title: How to configure Adaptive Sampling through IServiceCollection
body: <p>According to the <a href="https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling#configuring-adaptive-sampling-for-aspnet-core-applications" rel="nofollow noreferrer">docs</a>, under &quot;Configure sampling settings&quot;, we can configure adaptive sampling and also include or exclude certain types from sampling:</p>
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration configuration)
  {
      var builder = configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
      // For older versions of the Application Insights SDK, use the following line instead:
      // var builder = configuration.TelemetryProcessorChainBuilder;
  
      // Using adaptive sampling
      builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5);
  
      // Alternately, the following configures adaptive sampling with 5 items per second, and also excludes DependencyTelemetry from being subject to sampling.
      // builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: &quot;Dependency&quot;);
  
      // If you have other telemetry processors:
      builder.Use((next) =&gt; new AnotherProcessor(next));
      
      builder.Build();
  
      // ...
  }
  </code></pre>
  <p>However, is there a way to do the same thing if we're configuring our application through <code>WebHostBuilder.Configure()</code> and passing in a delegate that takes an <code>IApplicationBuilder</code>?  In this situation, we wouldn't be able to obtain the <code>TelemetryConfiguration</code> through DI.</p>
  <p>I need to do something similar as the above, but I've only got the <code>IApplicationBuilder</code> interface.  From the docs, it seems like I should use <code>TelemetryConfiguration.Active.DefaultTelemetrySink.TelemetryProcessorChainBuilder</code>, but <code>TelemetryConfiguration.Active</code> says it's obsolete on .NET Core and also says it loads the instance from ApplicationInsights.config, which I don't even have.  I'm creating an entire AppInsights client from code.</p>
  <p>I suppose another way to do it would be to create my own TelemetryProcessor that does sampling exactly the way I want, but this seems like overkill.  Basically I'd like to alter the configuration of the current <code>AdaptiveSamplingTelemetryProcessor</code> in the pipeline.</p>