azure - 应用程序洞察中间件 - 跟踪 SQL 参数

标签 azure azure-application-insights asp.net-core-3.1 .net-core-3.1

问题:默认情况下的应用洞察 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 中将参数视为自定义属性:

enter image description here

供引用:我在我的 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/

相关文章:

python-3.x - 使用 SAS token 将文件从 Amazon AWS s3 传输到 Azure Blob 的 python 脚本

asp.net-mvc-3 - Azure 上的空白页

azure - Application Insights - 如何排除 localhost

javascript - 客户端相对寻址并允许应用程序 URL 中没有尾部斜杠

c# - 用加号解码字符串

Java - 访问在 Windows Azure 上运行的 MongoDB 服务器

c# - 如何在使用 OWIN 的 WebApi 项目上启用 Application Insights 服务器遥测?

Azure Application Insights - 为异常创建深层链接?

c# - CORS 停止 Angular 应用程序和 .NET CORE 3.0 API 之间的通信

azure - 管理 Service Fabric 应用程序中的 secret