Enable Profiler for ASP.NET Core web applications hosted in Linux on App Services
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. While this guide focuses on web apps hosted in Linux, you can experiment using Linux, Windows, and Mac development environments.
In this guide, you'll:
- 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 and greatest .NET Core SDK.
- Install Git by following the instructions at Getting Started - Installing Git.
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
:public void ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights. services.AddServiceProfiler(); // Add this line of code to Enable Profiler services.AddControllersWithViews(); }
Add a line of code in the HomeController.cs section to randomly delay a few seconds:
using System.Threading; ... public IActionResult About() { Random r = new Random(); int delay = r.Next(5000, 10000); Thread.Sleep(delay); return View(); }
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 note of your credentials to use later.
Click Save.
Select the Settings tab.
In the drop-down, select Local Git to set up a local Git repository in the web app.
Click Save to create a Git repository with a Git Clone Uri.
For more deployment options, see App Service documentation.
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.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.
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 can add Application Insights to your web app either via:
- The Application Insights pane in the Azure portal,
- The Configuration pane in the Azure portal, or
- Manually adding to your web app settings.
In your web app on the Azure portal, select Application Insights in the left side menu.
Click 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'll create a new resource.
Click Apply > Yes to apply and confirm.
Next steps
Learn how to...
Feedback
Submit and view feedback for