如何在Application Insights中禁用SQL依赖项遥测
禁用Azure Application Insights的SQL依赖项日志
以下是几种有效的解决方法,按优先级排序:
方法1:通过Telemetry Processor过滤SQL依赖项
如果不想完全禁用所有依赖项追踪,仅过滤SQL相关日志,可以自定义Telemetry Processor:
- 创建一个实现
ITelemetryProcessor的类:
using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Extensibility; public class SqlDependencyFilter : ITelemetryProcessor { private ITelemetryProcessor Next { get; set; } public SqlDependencyFilter(ITelemetryProcessor next) { Next = next; } public void Process(ITelemetry item) { if (item is DependencyTelemetry dependency) { // 过滤SQL依赖项(根据Type或Name判断) if (dependency.Type == "SQL" || dependency.Name.Contains("SQL")) { return; // 不发送该日志 } } Next.Process(item); } }
- 在
ApplicationInsights.config中注册这个处理器:
<TelemetryProcessors> <Add Type="YourNamespace.SqlDependencyFilter, YourAssemblyName"/> </TelemetryProcessors>
方法2:通过配置文件禁用特定依赖项类型
如果要保留部分依赖项追踪,仅禁用SQL类型,先取消之前注释的DependencyTrackingTelemetryModule行,再添加过滤规则:
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector"> <ExcludeComponentCorrelationHttpHeadersOnDomains> <!-- 保留原有配置内容 --> </ExcludeComponentCorrelationHttpHeadersOnDomains> <IncludeDiagnosticSourceActivities> <!-- 移除或注释SQL相关的源,例如: --> <!-- <Add>Microsoft.EntityFrameworkCore</Add> --> <!-- <Add>System.Data.SqlClient</Add> --> </IncludeDiagnosticSourceActivities> <ExcludeTypes>SQL</ExcludeTypes> <!-- 添加此行,直接排除SQL类型依赖项 --> </Add>
方法3:排查注释配置无效的原因并彻底禁用依赖项追踪
若注释DependencyTrackingTelemetryModule后问题未解决,需检查以下几点:
- 代码手动注册模块:检查启动文件(如
Program.cs或Startup.cs),若存在configuration.TelemetryModules.Add(new DependencyTrackingTelemetryModule());这类代码,直接删除。 - 部署文件未更新:通过Kudu控制台查看Azure App Service站点目录(
site/wwwroot),确认修改后的ApplicationInsights.config已部署。 - SDK自动注册:若使用新版Application Insights SDK,可能通过
AddApplicationInsightsTelemetry自动注册模块,需在服务注册时禁用:
builder.Services.AddApplicationInsightsTelemetry(options => { options.EnableDependencyTrackingTelemetryModule = false; });
方法4:通过Azure门户配置采样或过滤
若代码/配置修改不便,可直接在Azure门户操作:
- 数据采样:进入Application Insights资源,在「使用情况和预估成本」>「数据采样」中,将依赖项数据的采样率设为0%,完全禁用SQL依赖日志采集。
- 保留策略:在「日志」中设置保留规则,自动清理旧的SQL依赖日志,减少存储空间占用。
内容的提问来源于stack exchange,提问作者Nele
相关产品推荐
相关产品推荐

