如何在 .NET Core 应用程序中控制自适应采样

How to control Adaptive Sampling in .NET Core application

我正在尝试限制在 .NET Core 程序中发送到 Application Insights 的数据量。我正在尝试遵循文档 here,其中说我应该使用 UseAdaptiveSampling 方法。它有相当神秘的指令:

Use extension methods of TelemetryProcessorChainBuilder as shown below to customize sampling behavior.

但是,它并没有告诉我 这个扩展方法究竟存在于何处。我的代码如下:

public AppInsightsStats(string appInsightsKey)
{
    TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
    configuration.InstrumentationKey = appInsightsKey;
    telemetry = new TelemetryClient(configuration);
    // Enable sampling since amount of logging is massive
    var builder = configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
    builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5); // <-- Compiler error here
}

但是,代码无法编译,因为未找到 UseAdaptiveSampling。我有以下 using 语句:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

我可以挖掘的使用 UseAdaptiveSampling 的源代码似乎都是 .NET Framework 4.5 代码,所以我想知道这是否不包含在 .NET Core 版本中。这些说明适用于 ASP.NET Core,但我想知道它们是否意味着 运行 在 Windows.

上的 .NET Framework 上

对于控制台项目,你应该使用这个包Microsoft.ApplicationInsights.WorkerService。它用于 non-http 控制台项目等应用程序。

我在这个包中使用了你的代码,一切正常。请试一试,如果问题仍然存在,请告诉我。