添加自定义属性的应用程序透视指标每个请求 [英] Adding custom properties for each request in Application Insights metrics

查看:185
本文介绍了添加自定义属性的应用程序透视指标每个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我D'喜欢自定义属性添加到由应用程序解析带到我的应用程序的每个请求指标。例如,我要添加的用户登录和租客code,比如我可以细分/组中的Azure门户的度量标准。

相关文档页面似乎是这一个:<一href=\"https://azure.microsoft.com/en-us/documentation/articles/app-insights-api-custom-events-metrics/\"相对=nofollow>设置默认属性值

但这个例子是一个事件(即 gameTelemetry.TrackEvent(WinGame); ),而不是一个HTTP请求:

  VAR背景=新TelemetryContext();
context.Properties [游戏] = currentGame.Name;
VAR gameTelemetry =新TelemetryClient(背景);
gameTelemetry.TrackEvent(WinGame);

我的问题:


  1. 什么是相关code代表的请求,因为我没有具体code。在这个时候(它似乎是由应用程序洞察SDK自动管理):仅仅是创建一个 TelemetryContext 是否足够?我也应该创建一个 TelemetryClient 如果是这样,我应该将其链接到当前的请求?怎么样?

  2. 我在哪里应该把这个code?它是确定在的的Application_BeginRequest 方法的Global.asax


解决方案

涉及到的第一个问题如何自定义事件添加到我的请求/什么是相关code的请求,我觉得主要的困惑这里涉及到的命名。

这是我们需要指出的第一件事是,有不同类型的信息,我们可以使用应用程序洞察捕捉:


  1. 自定义事件

  2. 请求

  3. 例外

  4. 跟踪

  5. 页面视图

  6. 依赖

一旦我们知道这一点,我们可以说,TrackEvent是关系到自定义事件,因为TrackRequest相关请求。

当我们要保存的要求,我们需要做的是以下内容:

  VAR要求=新RequestTelemetry();
 VAR的客户=新TelemetryClient();
 request.Name =我的请求;
 client.TrackRequest(countEvent);

因此​​,让我们想象一下,您的用户登录和租户code都是字符串。我们可以做一个新的请求只是用下面的code登录信息:

 公共无效LogUserNameAndTenant(用户名字符串,字符串租户code)
    {
        VAR要求=新RequestTelemetry();        request.Name =我的请求;
        request.Context.Properties [用户名] =用户名;
        request.Context.Properties [租客code] =租户code;        VAR的客户=新TelemetryClient();
        client.TrackRequest(请求);
    }

这样做只是TelemetryContext是不够的,因为我们需要一种方法来发送信息,而这也正是该TelemetryClient到位得到。

我希望它能帮助。

I d'like to add custom properties to metrics taken by Application Insights to each request of my app. For example, I want to add the user login and the tenant code, such as I can segment/group the metrics in the Azure portal.

The relevant doc page seems to be this one : Set default property values

But the example is for an event (i.e. gameTelemetry.TrackEvent("WinGame");), not for an HTTP request :

var context = new TelemetryContext();
context.Properties["Game"] = currentGame.Name;
var gameTelemetry = new TelemetryClient(context);
gameTelemetry.TrackEvent("WinGame");

My questions :

  1. What is the relevant code for a request, as I have no specific code at this time (it seems to be automatically managed by the App Insights SDK) : Is just creating a TelemetryContext sufficient ? Should I create also a TelemetryClient and if so, should I link it to the current request ? How ?
  2. Where should I put this code ? Is it ok in the Application_BeginRequest method of global.asax ?

解决方案

Related to the first question "how to add custom event to my request / what is the relevant code to a request", I think the main confusion here is related to the naming.

The first thing that we need to point out is that there are different kinds of information that we can capture with Application Insights:

  1. Custom Event
  2. Request
  3. Exception
  4. Trace
  5. Page View
  6. Dependency

Once we know this, we can say that TrackEvent is related to "Custom Events", as TrackRequest is related to Requests.

When we want to save a request, what we need to do is the following:

 var request = new RequestTelemetry();
 var client = new TelemetryClient();
 request.Name = "My Request";
 client.TrackRequest(countEvent);

So let's imagine that your user login and tenant code both are strings. We could make a new request just to log this information using the following code:

    public void LogUserNameAndTenant(string userName, string tenantCode)
    {
        var request = new RequestTelemetry();

        request.Name = "My Request";
        request.Context.Properties["User Name"] = userName;
        request.Context.Properties["Tenant Code"] = tenantCode;

        var client = new TelemetryClient();
        client.TrackRequest(request);
    }

Doing just a TelemetryContext will not be enough, because we need a way to send the information, and that's where the TelemetryClient gets in place.

I hope it helps.

这篇关于添加自定义属性的应用程序透视指标每个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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