繁体   English   中英

在Application Insights遥测(客户端)中生成版本信息

[英]Build version information in Application Insights telemetry (client-side)

我们有一个SPA托管在ASP.NET应用程序上。 我们要跟踪整个应用程序的构建版本。

所以这个遥测初始化器

public class VersionInfoTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.Component.Version =
          typeof(Startup).Assembly.GetName().Version.ToString();
    }
}

将在Gloabal.asax中使用

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        var tc = TelemetryConfiguration.Active;
        tc.InstrumentationKey = ConfigurationManager.AppSettings["AI Instrumentation Key"];
        tc.TelemetryInitializers.Add(new VersionInfoTelemetryInitializer());
        ...
    }
}

服务器端遥测将附加版本信息。 但是我无法对客户端遥测执行相同的操作。 我已经试过了

var appInsights = window.appInsights || function(config) {
    // standard js snippet from azure portal
}({
    instrumentationKey: '{{INSTRUMENTATIONKEY}}'
});
window.appInsights = appInsights;
window.appInsights.context.application.ver = 'some version number';

这导致以下JS错误

Uncaught TypeError: Cannot read property 'application' of undefined

我也试过

appInsights.queue.push(function () {
    appInsights.context.addTelemetryInitializer(versionInfoTelemetryInitialier);
});

function versionInfoTelemetryInitialier(envelope) {
    var telemetryItem = envelope.data.baseData;
    telemetry.context.component.version = 'some version number';
}

将会警告以下消息

AI: TelemetryInitializerFailed message:"One of telemetry initializers failed, telemetry
item will not be sent: TypeError" props:"{exception:[object Error]{ stack: 'TypeError:
Unable to get property 'component' of undefined or null reference\n   at
versionInfoTelemetryInitialier (https://localhost:44301/landing/index:107:9)\n   at
n.prototype._track (https://az416426.vo.msecnd.net/scripts/a/ai.0.js:1:65589)\n   at
n.prototype.track...

我应该怎么做才能使客户端遥测附加版本信息。

我认为您的第二次尝试非常接近。 您需要通过队列执行此操作,以确保它在所有AI脚本实际加载后发生,所以我认为这是正确的:

appInsights.queue.push(function () {
    appInsights.context.addTelemetryInitializer(versionInfoTelemetryInitialier);
});

但在你的初始化你切换context.application.ver在你的第一个例子,以context.component.version在你的第二个。

的JavaScript SDK被记录在github上回购: https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md

那里的例子显示:

 context.application.ver: string
 context.application.build : string

所以那个初始化方法不应该是:

function versionInfoTelemetryInitialier(envelope) {
   var telemetryItem = envelope.data.baseData;
   telemetry.context.application.ver = 'some version number';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM