建立 Application Insights 資源

警告

本文適用于 不再建議使用的 Application Insights 傳統資源。

本文中的資訊已過時,且不會更新。

轉換為工作區型的 Application Insights,以充分利用新功能

Application Insights 會顯示 Azure 資源中應用程式的相關資料。 建立新的資源是 設定 Application Insights 以監視新應用程式的一部分。 建立新資源之後,您可以取得其檢測金鑰,並使用它來設定 Application Insights SDK。 此檢測金鑰會將您的遙測連結至資源。

重要事項

2024 年 2 月 29 日,傳統 Application Insights 的支援將會結束轉換為工作區型的 Application Insights,以充分利用新功能。 2021 年 2 月之後引進的較新區域不支援建立傳統 Application Insights 資源。

注意

針對檢測金鑰擷取的支援將在 2025 年 3 月 31 日結束。 檢測金鑰擷取將會繼續運作,但我們將不再提供該功能的更新或支援。 轉換至連接字串以利用新功能

登入 Azure

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

建立 Application Insights 資源

登入Azure 入口網站並建立 Application Insights 資源。

此螢幕擷取畫面顯示選取左上角的 [+ 登入]、[開發人員工具] 和 [Application Insights]。

設定 描述
名稱 Unique value 識別您要監視之應用程式的名稱。
資源群組 myResourceGroup 要裝載 Application Insights 資料之新資源群組或現有資源群組的名稱。
區域 East US 選取您附近或靠近應用程式裝載的位置。
資源模式 ClassicWorkspace-based 工作區形式資源可讓您將 Application Insights 遙測傳送至通用 Log Analytics 工作區。 如需詳細資訊,請參閱 工作區型 Application Insights 資源

注意

您可以跨不同的資源群組使用相同的資源名稱,但使用全域唯一名稱會很有説明。 如果您打算 執行跨資源查詢,使用全域唯一名稱可簡化必要的語法。

在必要欄位中輸入適當的值。 選取 [檢閱 + 建立]。

顯示在必要欄位中輸入值的螢幕擷取畫面,以及 [檢閱 + 建立] 按鈕。

建立應用程式之後,新的窗格會顯示有關受監視應用程式的效能和使用方式資料。

複製檢測金鑰

檢測金鑰會識別您想要與遙測資料建立關聯的資源。 您必須複製檢測金鑰,並將其新增至應用程式的程式碼。

在應用程式中安裝 SDK

在應用程式中安裝 Application Insights SDK 核心。 此步驟高度仰賴於應用程式的類型。

使用檢測金鑰來設定您在應用程式中安裝的 SDK

SDK 包含傳送遙測的標準模組,因此您不需要撰寫更多程式碼。 若要更詳細追蹤使用者動作或診斷問題,請使用 API 來傳送您自己的遙測。

自動建立資源

使用 PowerShell 或 Azure CLI 自動建立資源。

PowerShell

建立新 Application Insights 資源。

New-AzApplicationInsights [-ResourceGroupName] <String> [-Name] <String> [-Location] <String> [-Kind <String>]
 [-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]

範例

New-AzApplicationInsights -Kind java -ResourceGroupName testgroup -Name test1027 -location eastus

結果

Id                 : /subscriptions/{subid}/resourceGroups/testgroup/providers/microsoft.insights/components/test1027
ResourceGroupName  : testgroup
Name               : test1027
Kind               : web
Location           : eastus
Type               : microsoft.insights/components
AppId              : 8323fb13-32aa-46af-b467-8355cf4f8f98
ApplicationType    : web
Tags               : {}
CreationDate       : 10/27/2017 4:56:40 PM
FlowType           :
HockeyAppId        :
HockeyAppToken     :
InstrumentationKey : 00000000-aaaa-bbbb-cccc-dddddddddddd
ProvisioningState  : Succeeded
RequestSource      : AzurePowerShell
SamplingPercentage :
TenantId           : {subid}

如需此 Cmdlet 的完整 PowerShell 文件,並了解如何擷取檢測金鑰,請參閱 Azure PowerShell 文件

Azure CLI (預覽)

若要存取預覽 Application Insights Azure CLI 命令,您必須先執行:

 az extension add -n application-insights

如果您未執行 az extension add 命令,您會看到錯誤訊息,指出: az : ERROR: az monitor: 'app-insights' is not in the 'az monitor' command group. See 'az monitor --help'.

執行下列命令以建立 Application Insights 資源:

az monitor app-insights component create --app
                                         --location
                                         --resource-group
                                         [--application-type]
                                         [--kind]
                                         [--tags]

範例

az monitor app-insights component create --app demoApp --location westus2 --kind web --resource-group demoRg --application-type web

結果

az monitor app-insights component create --app demoApp --location eastus --kind web --resource-group demoApp --application-type web
{
  "appId": "87ba512c-e8c9-48d7-b6eb-118d4aee2697",
  "applicationId": "demoApp",
  "applicationType": "web",
  "creationDate": "2019-08-16T18:15:59.740014+00:00",
  "etag": "\"0300edb9-0000-0100-0000-5d56f2e00000\"",
  "flowType": "Bluefield",
  "hockeyAppId": null,
  "hockeyAppToken": null,
  "id": "/subscriptions/{subid}/resourceGroups/demoApp/providers/microsoft.insights/components/demoApp",
  "instrumentationKey": "00000000-aaaa-bbbb-cccc-dddddddddddd",
  "kind": "web",
  "location": "eastus",
  "name": "demoApp",
  "provisioningState": "Succeeded",
  "requestSource": "rest",
  "resourceGroup": "demoApp",
  "samplingPercentage": null,
  "tags": {},
  "tenantId": {tenantID},
  "type": "microsoft.insights/components"
}

如需此命令的完整 Azure CLI 檔,以及瞭解如何擷取檢測金鑰,請參閱 Azure CLI 檔

覆寫預設端點

警告

請勿修改端點。 轉換至連接字串以簡化設定,而且不需要修改端點。

若要將資料從 Application Insights 傳送至特定區域,您需要覆寫預設端點位址。 每個 SDK 都需要稍微不同的修改,本文章會說明全部修改。

這些變更會要求您調整範例程式碼,並以特定區域的實際端點位址取代 、 TelemetryChannel_Endpoint_AddressProfile_Query_Endpoint_address 的預留位置值 QuickPulse_Endpoint_Address 。 本文章結尾包含需要此設定的區域本身的端點位址連結。

注意

針對檢測金鑰擷取的支援將在 2025 年 3 月 31 日結束。 檢測金鑰擷取將會繼續運作,但我們將不再提供該功能的更新或支援。 轉換至連接字串以利用新功能


SDK 程式碼變更

注意

每次執行 SDK 升級時,會自動覆寫 applicationinsights.config 檔案。 執行 SDK 升級之後,請務必重新輸入區域特定的端點值。

<ApplicationInsights>
  ...
  <TelemetryModules>
    <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector">
      <QuickPulseServiceEndpoint>Custom_QuickPulse_Endpoint_Address</QuickPulseServiceEndpoint>
    </Add>
  </TelemetryModules>
   ...
  <TelemetrySinks>
    <Add Name = "default">
      <TelemetryChannel>
         <EndpointAddress>TelemetryChannel_Endpoint_Address</EndpointAddress>
      </TelemetryChannel>
    </Add>
  </TelemetrySinks>
  ...
  <ApplicationIdProvider Type="Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ApplicationInsightsApplicationIdProvider, Microsoft.ApplicationInsights">
    <ProfileQueryEndpoint>Profile_Query_Endpoint_address</ProfileQueryEndpoint>
  </ApplicationIdProvider>
  ...
</ApplicationInsights>

需要端點修改的區域

目前需要修改端點的區域只有 Azure GovernmentAzure 中國

區域 端點名稱
Azure 中國 遙測通道 https://dc.applicationinsights.azure.cn/v2/track
Azure 中國 QuickPulse (即時計量) https://live.applicationinsights.azure.cn/QuickPulseService.svc
Azure 中國 設定檔查詢 https://dc.applicationinsights.azure.cn/api/profiles/{0}/appId
Azure Government 遙測通道 https://dc.applicationinsights.us/v2/track
Azure Government QuickPulse (即時計量) https://quickpulse.applicationinsights.us/QuickPulseService.svc
Azure Government 設定檔查詢 https://dc.applicationinsights.us/api/profiles/{0}/appId

如果您目前使用通常透過 api.applicationinsights.io 存取的 Application Insights REST API,則必須使用本機到您區域的端點。

區域 端點名稱
Azure 中國 REST API api.applicationinsights.azure.cn
Azure Government REST API api.applicationinsights.us

下一步