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

时间:2020-08-18 01:40:03

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

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

使用如下所示的TelemetryProcessorChainBuilder扩展方法来自定义采样行为。

但是,它并没有告诉我该的确切位置。我的代码如下:

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上运行它。

1 个答案:

答案 0 :(得分:1)

对于控制台项目,您应该使用此软件包Microsoft.ApplicationInsights.WorkerService。它用于非HTTP应用程序,例如控制台项目。

我在此软件包中使用了您的代码,一切正常。请尝试一下,如果您仍然遇到问题,请告诉我。