You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Azure Application Insights无法捕获SQL命令问题排查

Application Insights无法捕获SQL语句及失败原因的排查方案

问题背景

在Azure虚拟机上运行ASP.NET Core与ASP.NET混合架构的网站,均连接Azure SQL数据库。已配置Application Insights,除SQL语句捕获功能外其余功能正常:仅能看到SQL依赖事件,但无法获取具体执行的SQL语句;查询失败的SQL依赖时,也看不到失败原因。

排查与解决步骤

1. 确认Instrumentation Engine的实际运行状态

  • 查看Application Insights Agent日志(默认路径:C:\Program Files\Microsoft Application Insights Agent\Logs),检查是否存在SQL instrumentation相关的加载失败、权限报错等日志。
  • 对于ASP.NET应用,验证web.config中是否配置了正确的Instrumentation Engine参数,确保EnableSqlCommandTextInstrumentation已设为true,且AI密钥配置正确。

2. 区分ASP.NET与ASP.NET Core的配置差异

ASP.NET应用

  • 确保项目已安装Microsoft.ApplicationInsights.Web和Microsoft.ApplicationInsights.DependencyCollector NuGet包,且版本与Agent兼容。
  • 在ApplicationInsights.config中明确开启SQL命令文本收集:
    <TelemetryModules>
      <Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector">
        <EnableSqlCommandTextInstrumentation>true</EnableSqlCommandTextInstrumentation>
      </Add>
    </TelemetryModules>
    

ASP.NET Core应用

  • 在Program.cs或Startup.cs中手动启用SQL命令文本捕获:
    builder.Services.AddApplicationInsightsTelemetry(options =>
    {
        options.EnableSqlCommandTextInstrumentation = true;
    });
    
  • 确认Agent版本支持当前使用的.NET Core版本(Agent 2.8.10及以上支持.NET Core 3.1+)。

3. 检查SQL驱动与连接字符串

  • 确保应用使用的是System.Data.SqlClient(ASP.NET)或Microsoft.Data.SqlClient(ASP.NET Core)驱动,避免使用老旧的不支持追踪的驱动版本。
  • 优先使用最新稳定版的Microsoft.Data.SqlClient(如4.0+),并确认连接字符串中无禁用追踪的特殊参数。

4. 优化日志查询语句

  • 先查询所有SQL依赖,验证commandName字段是否包含SQL语句:
    dependencies | where type == "SQL" | project timestamp, name, commandName, success, resultCode, summary | take 20
    
  • 失败的SQL依赖可通过resultCode(错误码)和summary字段查看具体失败原因,避免仅过滤success != "True"却遗漏关键字段。

5. 验证权限与环境限制

  • 确认Azure VM的系统账户具备读取应用进程内存的权限(Instrumentation Engine需此权限注入追踪代码),域环境下检查组策略是否限制了进程注入操作。
  • 检查IIS应用池账户的权限,避免因权限不足导致追踪代码无法正常注入。

内容的提问来源于stack exchange,提问作者Greg Obleshchuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 05:22:29