ערוך

שתף באמצעות


Enable Profiler for ASP.NET Core web apps hosted in Linux

By using Application Insights Profiler for .NET, you can track how much time is spent in each method of your live ASP.NET Core web apps that are hosted in Linux on Azure App Service. This article focuses on web apps hosted in Linux. You can also experiment by using Linux, Windows, and Mac development environments.

In this article, you:

  • Set up and deploy an ASP.NET Core web application hosted on Linux.
  • Add Application Insights Profiler to the ASP.NET Core web application.

Prerequisites

Set up the project locally

  1. Open a command prompt window on your machine.

  2. Create an ASP.NET Core MVC web application:

    dotnet new mvc -n LinuxProfilerTest
    
  3. Change the working directory to the root folder for the project.

  4. Add the NuGet package to collect the Profiler traces:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. In your preferred code editor, enable Application Insights and the .NET Profiler in Program.cs. Add custom Profiler settings, if applicable.

    For WebAPI:

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

    For 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. Save and commit your changes to the local repository:

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

Create the Linux web app to host your project

  1. In the Azure portal, create a web app environment by using App Service on Linux.

    Screenshot that shows creating the Linux web app.

  2. Go to your new web app resource and select Deployment Center > FTPS credentials to create the deployment credentials. Make a note of your credentials to use later.

    Screenshot that shows creating the deployment credentials.

  3. Select Save.

  4. Select the Settings tab.

  5. In the dropdown, select Local Git to set up a local Git repository in the web app.

    Screenshot that shows view deployment options in a dropdown.

  6. Select Save to create a Git repository with a Git clone URI.

    Screenshot that shows setting up the local Git repository.

    For more deployment options, see the App Service documentation.

Deploy your project

  1. In your command prompt window, browse to the root folder for your project. Add a Git remote repository to point to the repository on App Service:

    git remote add azure https://<username>@<app_name>.scm.azurewebsites.net:443/<app_name>.git
    
    • Use the username that you used to create the deployment credentials.
    • Use the app name that you used to create the web app by using App Service on Linux.
  2. Deploy the project by pushing the changes to Azure:

    git push azure main
    

    You should see output similar to the following example:

    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
    ...
    

Add Application Insights to monitor your web app

You have three options to add Application Insights to your web app:

  • By using the Application Insights pane in the Azure portal.
  • By using the Configuration pane in the Azure portal.
  • By manually adding to your web app settings.
  1. In your web app on the Azure portal, select Application Insights on the left pane.

  2. Select Turn on Application Insights.

    Screenshot that shows turning on Application Insights.

  3. Under Application Insights, select Enable.

    Screenshot that shows enabling Application Insights.

  4. Under Link to an Application Insights resource, either create a new resource or select an existing resource. For this example, we create a new resource.

    Screenshot that shows linking Application Insights to a new or existing resource.

  5. Select Apply > Yes to apply and confirm.

Next steps