次の方法で共有


Azure App Service での .NET、Node.js、Python、Java アプリケーションのアプリケーション監視を有効にする

自動インストルメンテーション ("ランタイム" 監視とも呼ばれます) は、コードの変更や高度な構成を必要とせずに Azure App Service の Application Insights を有効にする最も簡単な方法です。 特定のシナリオに基づいて、手動インストルメンテーションによる高度な監視が必要かどうかを評価します。

インストルメンテーション キーのインジェストのサポートは、2025 年 3 月 31 日に終了します。 インストルメンテーション キーのインジェストは引き続き機能しますが、この機能の更新プログラムやサポートは提供されなくなります。 接続文字列に移行することで、新機能をご利用いただけます。

Application Insights を有効にする

重要

自動インストルメンテーションの監視と手動の SDK ベースのインストルメンテーションの両方が検出された場合は、手動のインストルメンテーション設定のみが受け付けられます。 この配置により、重複したデータが送信されるのを防ぐことができます。 詳細については、「 Application Insights と Azure App Service の統合のトラブルシューティング」を参照してください。

Azure portal での自動インストルメンテーション

  1. アプリ サービスの左側のナビゲーション メニューで [Application Insights] を選択し、[有効化] を選択します。

    [有効] が選ばれている [Application Insights] タブを示すスクリーンショット。

  2. このアプリケーション用に、新しいリソースを作成するか、既存の Application Insights リソースを選びます。

    [OK] を選んで新しいリソースを作成すると、[監視の設定を適用します] というプロンプトが表示されます。 [続行] を選ぶと、新しい Application Insights リソースがアプリ サービスにリンクされます。 その後、アプリ サービスが再起動します。

    [リソースの変更] ドロップダウンを示すスクリーンショット。

  3. 使用するリソースを指定した後、アプリケーションのプラットフォームごとのデータを Application Insights でどのように収集するかを選択できます。 ASP.NET Core の収集オプションは、[推奨] または [無効] です。

    アプリケーションのインストルメント化のセクションを示すスクリーンショット。

監視拡張機能/エージェントを手動でアップグレードする

バージョン 2.8.9 以降からアップグレードする

バージョン 2.8.9 からのアップグレードは自動的に実行され、追加の操作は必要ありません。 新しい監視ビットは、バックグラウンドでターゲット アプリ サービスに配信され、アプリケーションの再起動時に取得されます。

実行している拡張機能のバージョンを確認するには、https://yoursitename.scm.azurewebsites.net/ApplicationInsights に移動します。

実行している拡張機能のバージョンを確認するための URL パスを示すスクリーンショット。

バージョン 1.0.0 - 2.6.5 からのアップグレード

バージョン 2.8.9 以降では、プレインストールされたサイト拡張機能が使われます。 以前のバージョンを使用している場合は、次の 2 つの方法のいずれかを使用して更新できます。

  • Azure portal から有効にしてアップグレードする: App Service 用の Application Insights 拡張機能がインストールされている場合でも、UI には [有効] ボタンのみが表示されます。 バックグラウンドで、古いプライベート サイト拡張機能が削除されます。

  • PowerShell を使用してアップグレードする:

    1. プレインストールされたサイト拡張機能 ApplicationInsightsAgent を有効にするようにアプリケーション設定を指定します。 詳しくは、「PowerShell を使用して有効にする」をご覧ください。
    2. Application Insights extension for Azure App Service (Azure App Service 用 Application Insights 拡張機能) という名前のプライベート サイト拡張機能を手動で削除します。

2.5.1 より前のバージョンからアップグレードする場合は、ApplicationInsights DLL がアプリケーションの bin フォルダーから削除されたことを確認します。 詳細については、「 Application Insights と Azure App Service の統合のトラブルシューティング」を参照してください。

監視拡張機能/エージェントを構成する

現在、ASP.NET Core の監視拡張機能を構成するオプションは提供されていません。

クライアント側の監視を有効にする

アプリの設定 の有無に関係なく、APPINSIGHTS_JAVASCRIPT_ENABLED 収集の ASP.NET Core アプリではクライアント側の監視が既定で有効になります。

クライアント側の監視を無効にする場合:

  1. [設定]>[構成] を選択します。

  2. [アプリケーション設定] で、次の情報を使って新しいアプリケーション設定を作成します。

    • 名前: APPINSIGHTS_JAVASCRIPT_ENABLED
    • : false
  3. 設定を保存します。 アプリを再起動します。

監視の自動化

Application Insights でのテレメトリ収集を有効にするのに必要なのは、次のアプリケーション設定を設定することだけです。

Application Insights の設定が表示された App Service のアプリケーション設定を示すスクリーンショット。

アプリケーション設定の定義

アプリ設定の名前 定義 価値
ApplicationInsightsAgent_EXTENSION_VERSION メインの拡張機能で、実行時の監視を制御します。 ~2 (Windows の場合) または ~3 (Linux の場合)
XDT_MicrosoftApplicationInsights_Mode 既定のモードでは、最適なパフォーマンスを保証するために、重要な機能のみが有効になります。 disabled または recommended
XDT_MicrosoftApplicationInsights_PreemptSdk ASP.NET Core アプリの場合のみ。 Application Insights SDK で相互運用 (Interop) を有効にします。 拡張機能と SDK を並行して読み込み、それを使ってテレメトリを送信します。 (Application Insights SDK を無効にします。) 1

Azure Resource Manager を使用した App Service のアプリケーション設定

Azure App Service のアプリケーション設定は、Azure Resource Manager テンプレートを使用して管理および構成できます。 この手法は、Resource Manager オートメーションで新しい App Service リソースをデプロイするとき、または既存のリソースの設定を変更するときに使用できます。

App Service リソースに使われるアプリケーション設定 JSON の基本構造を次に示します。

      "resources": [
        {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "Application Insights Settings"
          },
          "properties": {
            "key1": "value1",
            "key2": "value2"
          }
        }
      ]

既定の Application Insights の設定で Resource Manager テンプレートを作成するには、Application Insights を有効にして新しい Web アプリを作成する場合のようにプロセスを開始します。

  1. Azure portal で、必要な情報を含む新しい Web アプリ リソースを作成します。 [監視とセキュリティで保護] タブで Application Insights を有効にします

  2. [確認と作成] に移動し、[自動化用のテンプレートのダウンロード] を選択します。 このオプションにより、必要な設定がすべて設定済みの、最新の Resource Manager テンプレートが生成されます。

    App Service Web アプリの作成メニューを示すスクリーンショット。

次のテンプレート例は、Windows で実行されている .NET 8 (LTS) アプリケーション用です。 アプリケーションの名前とサブスクリプション ID のmy-monitored-web-appaaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4eプレースホルダーを使用します。

直接コピーして使用 することは お勧めしません。 代わりに、上記の手順に基づいて独自のテンプレートを生成します。


展開してテンプレートの例を表示する
{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "subscriptionId": {
            "type": "string"
        },
        "resourceGroupName": {
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "___location": {
            "type": "string"
        },
        "hostingPlanName": {
            "type": "string"
        },
        "serverFarmResourceGroup": {
            "type": "string"
        },
        "alwaysOn": {
            "type": "bool"
        },
        "ftpsState": {
            "type": "string"
        },
        "autoGeneratedDomainNameLabelScope": {
            "type": "string"
        },
        "sku": {
            "type": "string"
        },
        "skuCode": {
            "type": "string"
        },
        "workerSize": {
            "type": "string"
        },
        "workerSizeId": {
            "type": "string"
        },
        "numberOfWorkers": {
            "type": "string"
        },
        "currentStack": {
            "type": "string"
        },
        "phpVersion": {
            "type": "string"
        },
        "netFrameworkVersion": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [
        {
            "apiVersion": "2022-03-01",
            "name": "[parameters('name')]",
            "type": "Microsoft.Web/sites",
            "___location": "[parameters('___location')]",
            "tags": null,
            "dependsOn": [
                "microsoft.insights/components/my-monitored-web-app",
                "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
            ],
            "properties": {
                "name": "[parameters('name')]",
                "siteConfig": {
                    "appSettings": [
                        {
                            "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
                            "value": "[reference('microsoft.insights/components/my-monitored-web-app', '2015-05-01').ConnectionString]"
                        },
                        {
                            "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                            "value": "~2"
                        },
                        {
                            "name": "XDT_MicrosoftApplicationInsights_Mode",
                            "value": "default"
                        }
                    ],
                    "metadata": [
                        {
                            "name": "CURRENT_STACK",
                            "value": "[parameters('currentStack')]"
                        }
                    ],
                    "phpVersion": "[parameters('phpVersion')]",
                    "netFrameworkVersion": "[parameters('netFrameworkVersion')]",
                    "alwaysOn": "[parameters('alwaysOn')]",
                    "ftpsState": "[parameters('ftpsState')]"
                },
                "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                "clientAffinityEnabled": true,
                "virtualNetworkSubnetId": null,
                "httpsOnly": true,
                "publicNetworkAccess": "Enabled",
                "autoGeneratedDomainNameLabelScope": "[parameters('autoGeneratedDomainNameLabelScope')]"
            },
            "resources": [
                {
                    "type": "Microsoft.Web/sites/basicPublishingCredentialsPolicies",
                    "apiVersion": "2022-09-01",
                    "name": "[concat(parameters('name'), '/scm')]",
                    "properties": {
                        "allow": true
                    },
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('name'))]"
                    ]
                },
                {
                    "type": "Microsoft.Web/sites/basicPublishingCredentialsPolicies",
                    "apiVersion": "2022-09-01",
                    "name": "[concat(parameters('name'), '/ftp')]",
                    "properties": {
                        "allow": true
                    },
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('name'))]"
                    ]
                }
            ]
        },
        {
            "apiVersion": "2018-11-01",
            "name": "[parameters('hostingPlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "___location": "[parameters('___location')]",
            "kind": "",
            "tags": null,
            "dependsOn": [],
            "properties": {
                "name": "[parameters('hostingPlanName')]",
                "workerSize": "[parameters('workerSize')]",
                "workerSizeId": "[parameters('workerSizeId')]",
                "numberOfWorkers": "[parameters('numberOfWorkers')]",
                "zoneRedundant": false
            },
            "sku": {
                "Tier": "[parameters('sku')]",
                "Name": "[parameters('skuCode')]"
            }
        },
        {
            "apiVersion": "2020-02-02-preview",
            "name": "my-monitored-web-app",
            "type": "microsoft.insights/components",
            "___location": "centralus",
            "tags": null,
            "dependsOn": [
                "newWorkspaceTemplate"
            ],
            "properties": {
                "ApplicationId": "[parameters('name')]",
                "Request_Source": "IbizaWebAppExtensionCreate",
                "Flow_Type": "Redfield",
                "Application_Type": "web",
                "WorkspaceResourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/DefaultResourceGroup-CUS/providers/Microsoft.OperationalInsights/workspaces/DefaultWorkspace-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e-CUS"
            }
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "newWorkspaceTemplate",
            "resourceGroup": "DefaultResourceGroup-CUS",
            "subscriptionId": "[parameters('subscriptionId')]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "apiVersion": "2020-08-01",
                            "name": "DefaultWorkspace-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e-CUS",
                            "type": "Microsoft.OperationalInsights/workspaces",
                            "___location": "centralus",
                            "properties": {}
                        }
                    ]
                }
            }
        }
    ]
}

PowerShell を使用して有効にする

PowerShell を使用してアプリケーションの監視を有効にするために必要な操作は、基になるアプリケーション設定の変更のみです。 次の例では、リソース グループ my-monitored-web-appmy-resource-group という Web サイトのアプリケーション監視を有効にします。 InstrumentationKey=012345678-abcd-ef01-2345-6789abcd接続文字列に送信されるデータを構成します。

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を始めるには、「Azure PowerShell をインストールする」を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

Windows の場合は、ApplicationInsightsAgent_EXTENSION_VERSIONを ~2 に設定します。

Linux の場合は、ApplicationInsightsAgent_EXTENSION_VERSIONを ~3 に設定します。

$app = Get-AzWebApp -ResourceGroupName "my-resource-group" -Name "my-monitored-web-app" -ErrorAction Stop
$newAppSettings = @{} # Case-insensitive hash map
$app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} # Preserve non-Application-Insights application settings.
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e"; # Set the Application Insights connection string
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~3"; # Enable the ApplicationInsightsAgent.
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop

トラブルシューティング

専用のトラブルシューティングに関する記事をご覧ください。

次のステップ