问题:默认情况下的应用洞察 does not track SQL parameters in dependencies telemetry 。我想启用参数跟踪或手动添加参数。
小背景:我有 .Net Core 2 应用程序和应用程序洞察。我正在读取请求正文并将其保存为使用 TelemetryInitializer
的自定义属性。我迁移到 .Net Core 3,在那里我无法再使用 TelemetryInitializer
作为 .Net Core 3 disabled synchronous IO 。描述了类似的问题here 。因此,我已经按照此 example 实现了应用程序洞察中间件。 。我使用 EF Core
进行所有数据库访问。
问题:我找到了这个 issue其中表示可以“访问 TelemetryInitializer 中的 SqlCommand 操作详细信息”。但如何使用应用程序洞察中间件来实现这一目标?
最佳答案
您不必为此编写 asp.net core 中间件。相反,您应该使用 TelemetryInitializer。给定一个像这样的初始化器:
public class CustomInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (!(telemetry is DependencyTelemetry supportedTelemetry))
return;
if (supportedTelemetry.Type == "SQL" && supportedTelemetry.TryGetOperationDetail("SqlCommand", out var command))
{
foreach (DbParameter parameter in ((SqlCommand)command).Parameters)
{
supportedTelemetry.Properties.Add(parameter.ParameterName, parameter.Value.ToString());
}
}
}
}
我不知道你为什么这么说
I migrated to .Net Core 3 where I was not able to use TelemetryInitializer anymore as .Net Core 3 disabled synchronous IO
我一直使用 TelemetryInitializers。
我在 App Insights 中将参数视为自定义属性:
供引用:我在我的 Controller 中使用了这段代码,基于 this tutorial :
using (var bc = new BloggingContext())
{
bc.Database.EnsureCreated();
await bc.Blogs.AddAsync(new Blog
{
Url = "www.blank.com",
Rating = 0
});
await bc.SaveChangesAsync();
}
关于azure - 应用程序洞察中间件 - 跟踪 SQL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64262114/