azure - 如何在 Net Core HostBuilder 上配置 Application Insights 采样?

标签 azure .net-core azure-application-insights

我正在使用 ApplicationInsights.WorkerService nuget 包构建 .Net Core 后台服务。有关采样配置的文档说引用此: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling#configure-sampling-settings

它显示了这一点:

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: "Dependency");

  // If you have other telemetry processors:
  builder.Use((next) => new AnotherProcessor(next));

  builder.Build();

  // ...
}

现在在 HostBuilder 上我没有看到任何可以为我提供 TelemetryConfiguration 的扩展方法,nuget 的源代码也没有它: https://github.com/microsoft/ApplicationInsights-aspnetcore/blob/develop/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs

那么如何在 HostBuilder 上获取 TelemetryConfiguration 或 TelemetryProcessorChainBuilder?目前它看起来像这样:

Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                    services.AddApplicationInsightsTelemetryWorkerService();
                });

最佳答案

您应该按如下方式使用它:

Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();

                    services.Configure<TelemetryConfiguration>((config)=>
                    {
                        var builder = config.DefaultTelemetrySink.TelemetryProcessorChainBuilder;

                        builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 5);
                        builder.Build();
                    }                    
                    );

                   // Your other code
                });

关于azure - 如何在 Net Core HostBuilder 上配置 Application Insights 采样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294559/

相关文章:

c# - 我可以告诉 Application Insights 对遥测进行下采样,但不对异常进行采样吗?

python-3.x - pyspark写入wasb blob存储容器

azure - 将 SSL 用于具有自定义域名的 Azure CDN

c# - CA1821 移除空终结器

azure - 将所有 AKS 微服务输送到单个 Application Insights 中,并能够按微服务名称进行筛选

c# - 如何在不使用 Azure 的情况下在控制台应用程序的 Visual Studio 2015 诊断工具窗口中查看 SQL 事件(Application Insights)?

Azure 逻辑应用 - 调用安全端点 (TLS)

azure - azure 中的 View sys.resource_usage 和 sys.resource_stats 中没有数据

asp.net - 使用 HttpClientFactory 安全吗?

.net - 如何使用.Net Core 1.0开发Android应用程序?