Linux でホストされている ASP.NET Core Web アプリに対して Profiler を有効にする

Profiler を使用すると、Azure App Service 上の Linux でホストされている稼働中の ASP.NET Core Web アプリの各メソッドの実行にかかった時間を追跡できます。 この記事では、Linux でホストされている Web アプリに焦点を当てて説明します。 Linux、Windows、Mac の開発環境を使用して実験することもできます。

この記事では、次の内容について説明します。

  • Linux でホストされている ASP.NET Core Web アプリケーションを設定してデプロイします。
  • Application Insights Profiler を ASP.NET Core Web アプリケーションに追加します。

前提条件

プロジェクトをローカルで設定する

  1. お使いのコンピューターでコマンド プロンプト ウィンドウを開きます。

  2. ASP.NET Core MVC Web アプリケーションを作成します。

    dotnet new mvc -n LinuxProfilerTest
    
  3. 作業ディレクトリをプロジェクトのルート フォルダーに変更します。

  4. プロファイラー トレースを収集する NuGet パッケージを追加します。

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. 任意のコード エディターで、Program.cs で Application Insights と Profiler を有効にします。 必要に応じて、カスタム Profiler 設定を追加します。

    WebAPIの場合:

    // Add services to the container.
    builder.Services.AddApplicationInsightsTelemetry();
    builder.Services.AddServiceProfiler();
    

    Workerの場合:

    IHost host = Host.CreateDefaultBuilder(args)
        .ConfigureServices(services =>
        {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.AddServiceProfiler();
    
            // Assuming Worker is your background service class.
            services.AddHostedService<Worker>();
        })
        .Build();
    
    await host.RunAsync();
    
  6. 変更内容をローカル リポジトリに保存し、コミットします。

    git init
    git add .
    git commit -m "first commit"
    

プロジェクトをホストする Linux Web アプリを作成する

  1. Azure portal で、App Service on Linux を使用して Web アプリ環境を作成します。

    Linux Web アプリの作成を示すスクリーンショット。

  2. 新しい Web アプリ リソースに移動し、[デプロイ センター]>[FTPS 資格情報] を選択してデプロイ資格情報を作成します。 後で使用するため資格情報をメモしておきます。

    デプロイの資格情報の作成を示すスクリーンショット。

  3. [保存] を選択します。

  4. [Settings](設定) タブを選択します。

  5. ドロップダウンで [ローカル Git] を選択し、Web アプリでローカル Git リポジトリを設定します。

    ドロップダウンでのデプロイ オプションの参照を示すスクリーンショット。

  6. [保存] を選択して、Git clone URI を使用して Git リポジトリを作成します。

    ローカル Git リポジトリの設定を示すスクリーンショット。

    デプロイ オプションの詳細については、App Service のドキュメントを参照してください。

プロジェクトのデプロイ

  1. コマンド プロンプト ウィンドウで、プロジェクトのルート フォルダーを参照します。 App Service のリポジトリを指すように Git リモート リポジトリを追加します。

    git remote add azure https://<username>@<app_name>.scm.azurewebsites.net:443/<app_name>.git
    
    • デプロイ資格情報を作成するために使用したユーザー名を使用します。
    • App Service on Linux を使用して Web アプリを作成するために使用したアプリ名を使用します。
  2. Azure に変更をプッシュし、プロジェクトをデプロイします。

    git push azure main
    

    次の例のような出力が表示されます。

    Counting objects: 9, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (8/8), done.
    Writing objects: 100% (9/9), 1.78 KiB | 911.00 KiB/s, done.
    Total 9 (delta 3), reused 0 (delta 0)
    remote: Updating branch 'main'.
    remote: Updating submodules.
    remote: Preparing deployment for commit id 'd7369a99d7'.
    remote: Generating deployment script.
    remote: Running deployment command...
    remote: Handling ASP.NET Core Web Application deployment.
    remote: ......
    remote:   Restoring packages for /home/site/repository/EventPipeExampleLinux.csproj...
    remote: .
    remote:   Installing Newtonsoft.Json 10.0.3.
    remote:   Installing Microsoft.ApplicationInsights.Profiler.Core 1.1.0-LKG
    ...
    

Application Insights を追加して Web アプリを監視する

Application Insights を Web アプリに追加するには、次の 3 つのオプションがあります。

  • Azure portal の [Application Insights] ウィンドウを使用する。
  • Azure portal の [構成] ウィンドウを使用する。
  • [Web アプリの設定] に手動で追加する。
  1. Azure portal の Web アプリで、左側のウィンドウで [Application Insights] を選択します。

  2. [Application Insights を有効にする] を選択します。

    Application Insights をオンにすることを示すスクリーンショット。

  3. [Application Insights][有効化] を選択します。

    Application Insights の有効化を示すスクリーンショット。

  4. [Application Insights リソースへのリンク] で、新しいリソースを作成するか、既存のリソースを選択します。 この例では、新しいリソースを作成します。

    Application Insights を新しいリソースか既存のリソースにリンクすることを示すスクリーンショット。

  5. [適用]>[はい] を選択して確定します。

次のステップ