您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

创建 Application Insights 资源Create an Application Insights resource

Azure Application Insights 在 Microsoft Azure 资源中显示有关应用程序的数据。Azure Application Insights displays data about your application in a Microsoft Azure resource. 因此,创建新资源是设置 Application Insights 以监视新应用程序中的一个环节。Creating a new resource is therefore part of setting up Application Insights to monitor a new application. 创建新资源后,可以获取其检测密钥并使用它来配置 Application Insights SDK。After you have created your new resource, you can get its instrumentation key and use that to configure the Application Insights SDK. 检测密钥会将遥测链接到资源。The instrumentation key links your telemetry to the resource.

登录到 Microsoft AzureSign in to Microsoft Azure

如果没有 Azure 订阅,请在开始之前创建一个免费帐户。If you don't have an Azure subscription, create a free account before you begin.

创建 Application Insights 资源Create an Application Insights resource

登录 Azure 门户,并创建 Application Insights 资源:Sign in to the Azure portal, and create an Application Insights resource:

单击左上角的“+”号。

设置Settings ValueValue 说明Description
名称Name Unique value 名称,用于标识要监视的应用。Name that identifies the app you are monitoring.
资源组Resource Group myResourceGroup 用于托管 App Insights 数据的新资源组或现有资源组的名称。Name for the new or existing resource group to host App Insights data.
区域Region East US 选择离你近的位置或离托管应用的位置近的位置。Choose a location near you, or near where your app is hosted.
资源模式Resource Mode ClassicWorkspace-basedClassic or Workspace-based 基于工作区的资源当前为公共预览版,用于将 Application Insights 遥测发送到常用 Log Analytics 工作区。Workspace-based resources are currently in public preview and allow you to send your Application Insights telemetry to a common Log Analytics workspace. 有关详细信息,请参阅有关基于工作区的资源的文章For more information, see the article on workspace-based resources.

备注

虽然可以在不同资源组中使用相同的资源名称,但使用全局唯一名称会有好处。While you can use the same resource name across different resource groups, it can be beneficial to use a globally unique name. 如果打算执行跨资源查询,这将很有用,因为它可以简化所需的语法。This can be useful if you plan to perform cross resource queries as it simplifies the required syntax.

在必填字段中输入适当的值,然后选择“查看 + 创建”。Enter the appropriate values into the required fields, and then select Review + create.

在必填字段中输入值,然后选择“查看 + 创建”。

创建应用后,将打开一个新窗格。When your app has been created, a new pane opens. 可以在此窗格中查看有关受监视应用程序的性能和使用情况数据。This pane is where you see performance and usage data about your monitored application.

复制检测密钥Copy the instrumentation key

检测密钥用于标识要与遥测数据关联的资源。The instrumentation key identifies the resource that you want to associate your telemetry data with. 你需要复制检测密钥并将其添加到应用程序的代码中。You will need to copy the instrumentation key and add it to your application's code.

单击并复制检测密钥

在应用中安装 SDKInstall the SDK in your app

在应用中安装 Application Insights SDK。Install the Application Insights SDK in your app. 此步骤在很大程度上依赖于应用程序的类型。This step depends heavily on the type of your application.

使用检测密钥来配置在应用程序中安装的 SDKUse the instrumentation key to configure the SDK that you install in your application.

SDK 包含无需编写任何其他代码即可发送遥测数据的标准模块。The SDK includes standard modules that send telemetry without you having to write any additional code. 若要跟踪用户操作或更细致地诊断问题,请使用 API 发送自己的遥测数据。To track user actions or diagnose issues in more detail, use the API to send your own telemetry.

自动创建资源Creating a resource automatically

PowerShellPowerShell

新建 Application Insights 资源Create a new Application Insights resource

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

示例Example

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

结果Results

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 文档For the full PowerShell documentation for this cmdlet, and to learn how to retrieve the instrumentation key consult the Azure PowerShell documentation.

Azure CLI(预览版)Azure CLI (preview)

若要访问预览版 Application Insights Azure CLI 命令,首先需要运行以下命令:To access the preview Application Insights Azure CLI commands, you first need to run:

 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'.If you don't run the az extension add command, you will see an error message that states: az : ERROR: az monitor: 'app-insights' is not in the 'az monitor' command group. See 'az monitor --help'.

现在,可以运行以下命令来创建 Application Insights 资源:Now you can run the following to create your Application Insights resource:

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

示例Example

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

结果Results

az monitor app-insights component create --app demoApp --location eastus --kind web -g 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 文档For the full Azure CLI documentation for this command, and to learn how to retrieve the instrumentation key consult the Azure CLI documentation.

后续步骤Next steps