.NET Framework 4.7.2项目启用Application Insights SQL命令文本采集问题
我在IIS部署的.NET Framework 4.7.2 MVC项目中,配置Application Insights的EnableSqlCommandTextInstrumentation以采集完整SQL命令时遇到问题。已完成以下操作:
- 按自动安装步骤配置Application Insights
- 参考微软官方文档的「高级SQL跟踪以获取完整SQL查询」部分,在
ApplicationInsights.config中添加配置:
<TelemetryModules> <Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector"> <EnableSqlCommandTextInstrumentation>true</EnableSqlCommandTextInstrumentation>
- 安装了最新版本的
Microsoft.Data.SqlClient(4.8.4)
Azure上的Application Insights能正常接收数据,但始终无法获取SQL命令详情。同时我完全无法理解文档中的这段内容:
In the preceding cases, the proper way of validating that the instrumentation engine is correctly installed is by validating that the SDK version of collected DependencyTelemetry is rddp. Use of rdddsd or rddf indicates dependencies are collected via DiagnosticSource or EventSource callbacks, so the full SQL query won't be captured.
翻译:
在上述场景中,验证检测引擎是否正确安装的标准方式是查看采集到的DependencyTelemetry的SDK版本是否为
rddp。如果版本是rdddsd或rddf,说明依赖项是通过DiagnosticSource或EventSource回调采集的,此时无法捕获完整SQL查询。
更新1
查询对比后发现:
- 能正常采集SQL命令的.NET Core应用中,DependencyTelemetry的SDK版本为
rdddsc:2.21.0-429 - 我的.NET Framework应用中版本为
rddf:2.15.0-44797
我已在WebView项目和业务项目中安装了Microsoft.Data.SqlClient包,仅业务项目的app.config中有System.Data.SqlClient相关配置:
<entityFramework> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>
我将其替换为Microsoft.Data.SqlClient,但SDK版本仍显示为rddf:2.15.0-44797。
更新2及解决方案
问题根源确认是项目使用了System.Data.SqlClient。将所有项目从.NET Framework 4.7.2升级到4.8,并完成以下步骤后问题解决:
- 在包含DbContext的项目中安装NuGet包:
Install-Package ErikEJ.EntityFramework.SqlServer -Version 1.0.0-rc5
- 为DbContext类添加数据注解:
[DbConfigurationType(typeof(System.Data.Entity.SqlServer.MicrosoftSqlDbConfiguration))]
- 修改项目
app.config中的Entity Framework提供程序,替换原System.Data.SqlClient配置:
<entityFramework> <providers> <provider invariantName="Microsoft.Data.SqlClient" type="System.Data.Entity.SqlServer.MicrosoftSqlProviderServices, ErikEJ.EntityFramework.SqlServer" /> </providers> </entityFramework>
- 将项目中所有
using System.Data.SqlClient语句替换为using Microsoft.Data.SqlClient(例如引用SqlParameter等类型时)。
内容的提问来源于stack exchange,提问作者vlayk

