У меня возникла проблема с пакетом SDK Microsoft Application Insights для JavaScript, который был закрыт / исправлен некоторое время назад: https://github.com/Microsoft/ApplicationInsights-JS/issues/282
Я создал новое приложение Angular, используя Angular CLI. Затем я внес эти изменения, следуя эту статью.
Добавлен сервис мониторинга:
import {Injectable} from '@angular/core';
import {AppInsights} from 'applicationinsights-js';
@Injectable()
export class MonitoringService {
private config: Microsoft.ApplicationInsights.IConfig = {
instrumentationKey: 'KEY_GOES_HERE',
enableDebug: true,
verboseLogging: true
};
constructor() {
if (!AppInsights.config) {
AppInsights.downloadAndSetup(this.config);
}
}
logPageView(name?: string, url?: string, properties?: any, measurements?: any, duration?: number) {
AppInsights.trackPageView(name, url, properties, measurements, duration);
}
logEvent(name: string, properties?: any, measurements?: any) {
AppInsights.trackEvent(name, properties, measurements);
}
trackException(exception: Error) {
AppInsights.trackException(exception);
}
}
Добавил в свой app.component.ts
:
import {Component, OnInit} from '@angular/core';
import {MonitoringService} from './monitoring.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [MonitoringService]
})
export class AppComponent implements OnInit {
title = 'app works!';
constructor(private monitoringService: MonitoringService) { }
ngOnInit() {
this.monitoringService.logPageView();
}
throwAnException() {
this.monitoringService.trackException(new Error('manually track exception'));
throw 'this should appear in app insights'; // but it doesn't
}
}
Сделал простую кнопку для выброса исключения в моем app.component.html
:
<h1>
{{title}}
</h1>
<div (click)="throwAnException()">Click to throw an exception</div>
Работает регистрация просмотра страницы, а также отслеживание исключения путем явного вызова trackException
. При чтении документации и различных статей у меня сложилось впечатление, что неперехваченные исключения всегда автоматически отправляются в Application Insights. Однако я не вижу никого из них на портале.
Что я мог здесь упустить?
Используя эти версии:
applicationinsights-js: 1.0.11
@types/applicationinsights-js: 1.0.4