如何在 .Net Core 3.1 Web 应用程序中关闭 Application Insights 性能计数器和跟踪遥测

Posted

技术标签:

【中文标题】如何在 .Net Core 3.1 Web 应用程序中关闭 Application Insights 性能计数器和跟踪遥测【英文标题】:How to turn off Application Insights performance counters and tracking telemetry in .Net Core 3.1 web app 【发布时间】:2022-01-15 04:22:46 【问题描述】:

? 我试图像这样禁用这些模块:

ApplicationInsightsServiceOptions aiOptions = new ApplicationInsightsServiceOptions();

aiOptions.EnablePerformanceCounterCollectionModule = false;
aiOptions.EnableDependencyTrackingTelemetryModule = false;

services.AddApplicationInsightsTelemetry(aiOptions);

但这似乎不起作用。

【问题讨论】:

你为什么不直接卸载 AI NuGet 包? 因为我希望它们仅在暂存环境中被关闭。 您的代码似乎在做正确的事情。您如何确认它不起作用? 因为重新部署后AI应用中还有性能计数器。 【参考方案1】: 如图所示修改您的 startup.cs。您有两种选择。

第一个完全禁用性能计数器。

public void ConfigureServices(IServiceCollection services)

    var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ImplementationType == typeof(PerformanceCollectorModule));
    services.Remove(serviceDescriptor);

或第二个会删除单个计数器,您可以稍后添加自己的计数器。

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

    var modules = app.ApplicationServices.GetServices<ITelemetryModule>();
    var perfModule = modules.OfType<PerformanceCollectorModule>().First();
    perfModule.DefaultCounters.Clear();

更多详情请参考Disabling Application Insights for ASP.NET Core with Visual Studio、Removing TelemetryInitializers、EnableRequestTrackingTelemetryModule和SO。

【讨论】:

以上是关于如何在 .Net Core 3.1 Web 应用程序中关闭 Application Insights 性能计数器和跟踪遥测的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)?

如何将 Log4Net 日志发送到 Application Insights for .NET Core 3.1 Web 应用程序?

Wcf 服务在 .NET Core 3.1 控制台应用程序中工作,但在 ASP.NET Core 3.1 Web API 中无法工作

通过 Azure Devops 将 .Net Core 3.1 Web 应用程序部署到 Azure Linux Web 服务时出错

Asp.net core 3.1 保护 API 和 Web 应用程序

ASP.NET Core 3.1 - 如何获取客户端的 IP 地址?