This is an example app to enable Azure Monitor Profiler in ASP.NET Core WebAPI with Azure Monitor OpenTelemetry distro.
Using Application Insights ASP.NET Core 3.x instead? See the aspnetcore-aisdk3 example.
Here are the steps to build this example step to step.
Run this command to create a web api application:
dotnet new web -n SimpleApp -o . -f net8.0Add the following 2 packages, refer to SimpleApp.csproj for details:
<ItemGroup>
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="[1.*-*, 2.0.0)" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.Profiler" Version="[1.*-*, 2.0.0)" />
</ItemGroup>Restore the packages:
> dotnet restore SimpleApp.csprojOptionally, check the resolved package version
> dotnet list package
Project 'SimpleApp' has the following package references
[net8.0]:
Top-level Package Requested Resolved
> Azure.Monitor.OpenTelemetry.AspNetCore [1.*-*, 2.0.0) 1.3.0-beta.2
> Azure.Monitor.OpenTelemetry.Profiler [1.*-*, 2.0.0) 1.0.0-beta1
> Microsoft.VisualStudioEng.MicroBuild.Core 1.0.0 1.0.0Refer to Program.cs for more details:
builder.Services.AddOpenTelemetry()
.UseAzureMonitor() // Enable Azure Monitor OpenTelemetry distro for ASP.NET Core
.AddAzureMonitorProfiler(); // Add Azure Monitor ProfilerIf you run the code now, an exception will throw:
Unhandled exception. System.InvalidOperationException: A connection string was not found. Please set your connection string.Refer to connection string section to see options. Here's a quick way for local test using environment variable in PowerShell:
$env:APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=5d..."Tips: Copy the connection string from the overview blade of the application insights resource.
app.MapGet("/", async () =>
{
await Task.Delay(2000); // 2 seconds delay
return "Hello World!";
});dotnet runGenerate some traffic for profiler to capture the issue:
curl http://localhost:5082