[Application Insights]在Windows Desktop中使用Application Insights API

  • 73
  • 0

在Windows Desktop中使用Application Insights API

Visual Studio 2015目前支援在Web Application專案中快速加上Application Insights的支援,只要在Solution Explorer中的專案按下右鍵,在選單中就可以選擇Applicatoin Insights建立精靈以支援設定。

Applicatoin Insights建立精靈

這個精靈會幫你在專案的Reference中加入Application Insights的組件,並加入一個Appication Insights設定檔-ApplicationInsights.config。透過這些設定,就可以用來監測你的Web Application,自動偵測效能異常,並將這些資訊送至Azure平台上,讓你可以使用強大的分析工具診斷問題。

不過,如果使用Console, Class Library, Windows Form等專案,就無法透過這種方式建立Application Insights服務。這時,只能使用手動的方式自行加入。

1.建立Application Insights Resource

首先,一定要先有Application Insights Resource。登入Azure Portal後,在MARKETPLACE中選擇Monitoring+Management => Application Insights

輸入設定值後,就可以建立一個Application Insights Resource

2.加入ApplicationInsights Package

在Visaul Studio中,使用Nuget安裝Microsoft.ApplicationInsights.WindowsServer

如果只需要使用API,而不使用標準的遙測模組時,就安裝Microsoft.ApplicationInsights

3.使用TelemetryClient物件

在Portal中取得InstrumentationKey

在Code中設定InstrumentationKey,就可以使用Application Insights API了

TelemetryClient tc = new TelemetryClient();
tc.InstrumentationKey = "key copied from portal";            
tc.TrackEvent("Lab AI in Console");
            
tc.Flush(); // only for desktop apps