Saya mengalami masalah dengan Microsoft Application Insights SDK untuk JavaScript yang ditutup/diperbaiki beberapa waktu lalu: https://github.com/Microsoft/ApplicationInsights-JS/issues/282
Saya membuat aplikasi Angular baru menggunakan Angular CLI. Lalu saya membuat perubahan ini, mengikuti artikel ini.
Menambahkan layanan pemantauan:
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);
}
}
Menambahkannya ke app.component.ts
saya:
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
}
}
Membuat tombol sederhana untuk melemparkan pengecualian di app.component.html
saya:
<h1>
{{title}}
</h1>
<div (click)="throwAnException()">Click to throw an exception</div>
Mencatat tampilan halaman berfungsi, seperti halnya melacak pengecualian dengan memanggil trackException
secara eksplisit. Dari membaca dokumentasi dan berbagai artikel, saya mendapat kesan bahwa pengecualian yang tidak tertangkap akan selalu dikirim secara otomatis ke Application Insights. Namun, saya tidak melihat satupun yang muncul di portal.
Apa yang mungkin saya lewatkan di sini?
Menggunakan versi ini:
applicationinsights-js: 1.0.11
@types/applicationinsights-js: 1.0.4