在Application Insights指标中为每个请求添加自定义属性

JYL*_*JYL 28 c# asp.net azure-web-sites azure-application-insights

我喜欢将自定义属性添加到Application Insights 我的应用程序的每个请求采取的指标.例如,我想添加用户登录和租户代码,例如我可以在Azure门户中对指标进行分段/分组.

相关的doc页面似乎就是这样:设置默认属性值

但是示例是针对事件(即gameTelemetry.TrackEvent("WinGame");),而不是针对HTTP请求:

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

我的问题:

  1. 什么是请求的相关代码,因为我目前没有特定的代码(它似乎由App Insights SDK自动管理):只是创建了TelemetryContext足够的代码吗?我是否应该创建一个TelemetryClient,如果是,我应该将它链接到当前请求吗?怎么样 ?
  2. 我应该把这段代码放在哪里?它的Application_BeginRequest方法可以global.asax吗?

小智 19

它看起来像增加新的特性,以现有的要求可能使用ITelemetryInitializer提到这里.

我创建了如下所示的示例类,并添加了名为"LoggedInUser"的新属性来请求遥测.

public class CustomTelemetry : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        if (requestTelemetry == null) return;
        requestTelemetry.Properties.Add("LoggedInUserName", "DummyUser");

    }
}
Run Code Online (Sandbox Code Playgroud)

在应用程序启动事件中注册此类.下面的示例是从我创建的示例MVC应用程序中划分出来的

 public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        TelemetryConfiguration.Active.TelemetryInitializers
    .Add(new CustomTelemetry());
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以看到自定义属性"LoggedInUserName"显示在自定义请求属性组下.(请参阅下面的屏幕抓取)

Appinsight定制属性


Pli*_*iyo 13

与第一个问题"如何向我的请求添加自定义事件/请求的相关代码是什么"相关,我认为这里的主要混淆与命名有关.

我们需要指出的第一件事是,我们可以使用Application Insights捕获不同类型的信息:

  1. 自定义事件
  2. 请求
  3. 例外
  4. 跟踪
  5. 页面预览
  6. 依赖

一旦我们知道了这一点,我们可以说TrackEvent与"自定义事件"相关,因为TrackRequest与请求相关.

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

 var request = new RequestTelemetry();
 var client = new TelemetryClient();
 request.Name = "My Request";
 client.TrackRequest(request);
Run Code Online (Sandbox Code Playgroud)

因此,让我们假设您的用户登录和租户代码都是字符串.我们可以使用以下代码创建一个新请求来记录此信息:

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);
}
Run Code Online (Sandbox Code Playgroud)

仅仅使用TelemetryContext是不够的,因为我们需要一种方法来发送信息,这就是TelemetryClient到位的地方.

我希望它有所帮助.


Dee*_*101 6

您可以使用 staticHttpContext.CurrentItems字典作为短期(接近无状态)存储空间,以通过自定义将自定义属性值传递到默认遥测处理程序中。ITelemetryInitializer

实施处理程序

class AppInsightCustomProps : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;

        var httpCtx = HttpContext.Current;
        if (httpCtx != null)
        {
            var customPropVal = (string)httpCtx.Items["PerRequestMyCustomProp"];
            if (!string.IsNullOrWhiteSpace(customPropVal))
            {
                requestTelemetry.Properties["MyCustomProp"] = customPropVal;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

把它钩进去。把这个放进Application_Startglobal.asax.cs

TelemetryConfiguration.Active.TelemetryInitializers.Add(new AppInsightCustomProps());
Run Code Online (Sandbox Code Playgroud)

对所需的自定义属性进行编程,请求管道中的任何位置都有类似的内容

if (HttpContext.Current != null)
{
    HttpContext.Current.Items["PerRequestMyCustomProp"] = myCustomPropValue;
}
Run Code Online (Sandbox Code Playgroud)


小智 0

在该文档中,向下滚动几行,找到有关创建 IContextInitializer 实现的位置。您可以在遥测开始滚动之前调用的任何方法中调用它。

您的自定义属性将添加到所有事件、异常、指标、请求等所有内容中。

  • 我还要补充一点,当调用 Track 并且 AI 在结束请求时调用 Track 时,会添加属性。 (2认同)