Enable Profiler for ASP.NET Core web apps hosted in Linux
By using Profiler, 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
- Install the latest .NET Core SDK.
- Install Git by following the instructions at Getting started: Installing Git.
- Review the following samples for context:
Set up the project locally
Open a command prompt window on your machine.
Create an ASP.NET Core MVC web application:
dotnet new mvc -n LinuxProfilerTest
Change the working directory to the root folder for the project.
Add the NuGet package to collect the Profiler traces:
dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
In your preferred code editor, enable Application Insights and 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();
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
In the Azure portal, create a web app environment by using App Service on Linux.
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.
Select Save.
Select the Settings tab.
In the dropdown, select Local Git to set up a local Git repository in the web app.
Select Save to create a Git repository with a Git clone URI.
Deploy your project
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.chinacloudsites.cn: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.
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.
In your web app on the Azure portal, select Application Insights on the left pane.
Select Turn on Application Insights.
Under Application Insights, select Enable.
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.
Select Apply > Yes to apply and confirm.