将自定义维度添加到默认的“用于应用程序的请求遥测"服务的最简单方法是什么? [英] What is the easiest way to add custom dimensions to default Request Telemetry for App service?

查看:82
本文介绍了将自定义维度添加到默认的“用于应用程序的请求遥测"服务的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是利用默认的Application Insights日志记录来记录请求遥测,而无需任何自定义代码.请求遥测看起来像这样:

 时间戳[UTC] 2019-12-19T00:22:10.2563938Zid | a758472d124b6e4688a33b2ad9755f33.b3979544_名称GET MyMethod [类型]网址https://xxxx成功真实resultCode 200持续时间153.2676PerformanceBucket< 250msitemType请求customDimensionsAppId xxxx-xxxx-xxxx-xxxx-xxxxxxAspNetCoreEnvironment:美国西部_MS.ProcessedByMetricExtractors(名称:请求",版本:"1.1") 

现在,我想向请求遥测"中的customDimensions添加一个新属性,例如,correlationId.最简单的方法是什么?我只想扩展现有的请求遥测,不想创建新事件.

解决方案

要添加自定义尺寸,可以使用

如果您使用其他编程语言,请遵循官方文档并为 ITelemetryInitializer 使用正确的方法.

I just leverage default Application Insights logging to log the request telemetry without ANY custom code. The request telemetry looks like this:

timestamp [UTC]     2019-12-19T00:22:10.2563938Z
id                  |a758472d124b6e4688a33b2ad9755f33.b3979544_
name                GET MyMethod [type]
url                 https://xxxx
success             True
resultCode          200
duration            153.2676
performanceBucket   <250ms
itemType            request
customDimensions 
    AppId                           xxxx-xxxx-xxxx-xxxx-xxxxxx
    AspNetCoreEnvironment:          west us
   _MS.ProcessedByMetricExtractors (Name:'Requests', Ver:'1.1')

Now I want to add a new property to customDimensions in Request telemetry, say, correlationId. What is the easiest way to to it? I just want to expend the existing request telemetry, don't want to create new event.

解决方案

For adding custom dimensions, you can take use of ITelemetryInitializer.

Here is an example for a .NET core web project:

1.Add a class named MyTelemetryInitializer to the project, and the code like below:

public class MyTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;

        //if it's not a request, just return.
        if (requestTelemetry == null) return;

        if (!requestTelemetry.Properties.ContainsKey("correlationId"))
        {
            requestTelemetry.Properties.Add("correlationId", "id_123456");
        }
    }

 }

2.In Startup.cs -> ConfigureServices method, use the following code:

    public void ConfigureServices(IServiceCollection services)
    {
       //your other code           

        //register the class MyTelemetryInitializer.
        services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
    }

The test result:

If you're using other programming language, please follow the official doc and use the proper method for ITelemetryInitializer.

这篇关于将自定义维度添加到默认的“用于应用程序的请求遥测"服务的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆