Application Insights を使用して要求を追跡するコードを記述する

Azure portal の [パフォーマンス] ページでアプリケーションのプロファイルを表示するには、Application Insights を使用してアプリケーションの要求を追跡する必要があります。 すでにインストルメント化されているフレームワーク (ASP.NET や ASP.NET Core など) 上に構築されているアプリケーションの場合、Application Insights を使用して自動的に要求を追跡することができます。

その他のアプリケーション (Azure Cloud Services の worker ロールや Azure Service Fabric のステートレス API など) では、Application Insights に要求の開始位置と終了位置を伝えるコードを使用して要求を追跡する必要があります。 要求テレメトリはその後 Application Insights に送信され、[パフォーマンス] ページで確認できるようになります。 プロファイルはこれらの要求に対して収集されます。

要求を手動で追跡するには、次の操作を行います。

  1. アプリケーションの有効期間の初期段階に次のコードを追加します。

    using Microsoft.ApplicationInsights.Extensibility;
    ...
    // Replace with your own Application Insights instrumentation key.
    TelemetryConfiguration.Active.InstrumentationKey = "00000000-0000-0000-0000-000000000000";
    

    このグローバル インストルメンテーション キーの構成については、「Using Service Fabric with Application Insights」(Service Fabric と Application Insights を使用する) を参照してください。

  2. インストルメント化するすべてのコードについて、次の例のように、その近くに StartOperation<RequestTelemetry> using ステートメントを追加します。

    using Microsoft.ApplicationInsights;
    using Microsoft.ApplicationInsights.DataContracts;
    ...
    var client = new TelemetryClient();
    ...
    using (var operation = client.StartOperation<RequestTelemetry>("Insert_Your_Custom_Event_Unique_Name"))
    {
      // ... Code I want to profile.
    }
    
  3. 別の StartOperation<RequestTelemetry> スコープ内で StartOperation<RequestTelemetry> を呼び出すことはサポートされていません。 代わりに、入れ子にしたスコープで StartOperation<DependencyTelemetry> を使用できます。 次に例を示します。

    using (var getDetailsOperation = client.Operation<RequestTelemetry>("GetProductDetails"))
    {
      try
      {
        ProductDetail details = new ProductDetail() { Id = productId };
        getDetailsOperation.Telemetry.Properties["ProductId"] = productId.ToString();
    
        // By using DependencyTelemetry, 'GetProductPrice' is correctly linked as part of the 'GetProductDetails' request.
        using (var getPriceOperation = client.StartOperation<DependencyTelemetry>("GetProductPrice"))
        {
            double price = await _priceDataBase.GetAsync(productId);
            if (IsTooCheap(price))
            {
                throw new PriceTooLowException(productId);
            }
            details.Price = price;
        }
    
        // Similarly, note how 'GetProductReviews' doesn't establish another RequestTelemetry.
        using (var getReviewsOperation = client.StartOperation<DependencyTelemetry>("GetProductReviews"))
        {
            details.Reviews = await _reviewDataBase.GetAsync(productId);
        }
    
        getDetailsOperation.Telemetry.Success = true;
        return details;
      }
      catch(Exception ex)
      {
        getDetailsOperation.Telemetry.Success = false;
    
        // This exception gets linked to the 'GetProductDetails' request telemetry.
        client.TrackException(ex);
        throw;
      }
    }
    

注意

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

次のステップ

Application Insights Profiler のトラブルシューティングを行います。