可以通过 npm 命令行安装 Application Insights SDK(需要 Node.js 环境):
npm install --save applicationinsights-js
import { ApplicationInsights } from '@microsoft/applicationinsights-web'
angular.module('myApp').run(['$rootScope', function ($rootScope) {
const appInsights = new ApplicationInsights({
config: {
instrumentationKey: 'your_instrumentation_key'
}
})
appInsights.loadAppInsights()
$rootScope.$on('$routeChangeSuccess', function (event, current) {
appInsights.trackPageView({ name: current.name, uri: `/#!/${current.originalPath}` })
})
}])
在您的代码中,使用以下方法将日志记录发送到 Application Insights:
var appInsights = window.appInsights;
// 日志发送成功
appInsights.trackEvent({ name: "myEvent", properties: { prop1: "foo", prop2: "bar" } });
// 日志发送失败 (例如,应用程序处于离线状态)
appInsights.trackException(new Error("error message"), "handledAt: " + new Date());