.NET Profiler inschakelen in Azure-containers
U kunt Application Insights Profiler voor .NET inschakelen voor toepassingen die in uw container bijna zonder code worden uitgevoerd. Als u .NET Profiler wilt inschakelen voor uw containerinstantie, moet u het volgende doen:
- Voeg de verwijzing naar het
Microsoft.ApplicationInsights.Profiler.AspNetCore
NuGet-pakket toe. - Werk de code bij om profiler voor .NET in te schakelen.
- Stel de Application Insights-instrumentatiesleutel in.
In dit artikel krijgt u informatie over de verschillende manieren waarop u het volgende kunt doen:
- Installeer het NuGet-pakket in het project.
- Stel de omgevingsvariabele in via de orchestrator (zoals Kubernetes).
- Meer informatie over beveiligingsoverwegingen voor productie-implementatie, zoals het beveiligen van uw Application Insights-instrumentatiesleutel.
Vereisten
- Een Application Insights-resource. Noteer de instrumentatiesleutel.
- Docker Desktop om Docker-installatiekopieën te bouwen.
- .NET 6 SDK geïnstalleerd.
De omgeving instellen
Kloon en gebruik het volgende voorbeeldproject:
git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
Ga naar het container-app-voorbeeld:
cd examples/EnableServiceProfilerForContainerAppNet6
Dit voorbeeld is een barebones-project dat is gemaakt door de volgende CLI-opdracht aan te roepen:
dotnet new mvc -n EnableServiceProfilerForContainerApp
We hebben vertraging in het
Controllers/WeatherForecastController.cs
project toegevoegd om het knelpunt te simuleren.[HttpGet(Name = "GetWeatherForecast")] public IEnumerable<WeatherForecast> Get() { SimulateDelay(); ... // Other existing code. } private void SimulateDelay() { // Delay for 500ms to 2s to simulate a bottleneck. Thread.Sleep((new Random()).Next(500, 2000)); }
Voeg het NuGet-pakket toe om de .NET Profiler-traceringen te verzamelen:
dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
Schakel Application Insights en de .NET Profiler in.
Toevoegen
builder.Services.AddApplicationInsightsTelemetry()
enbuilder.Services.AddServiceProfiler()
na deWebApplication.CreateBuilder()
methode inProgram.cs
:var builder = WebApplication.CreateBuilder(args); builder.Services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights. builder.Services.AddServiceProfiler(); // Add this line of code to enable Profiler builder.Services.AddControllersWithViews(); var app = builder.Build();
Haal de nieuwste ASP.NET Core build/runtime-installatiekopieën op
Ga naar de .NET Core 6.0-voorbeeldmap:
cd examples/EnableServiceProfilerForContainerAppNet6
Haal de nieuwste ASP.NET Core-installatiekopieën op:
docker pull mcr.microsoft.com/dotnet/sdk:6.0 docker pull mcr.microsoft.com/dotnet/aspnet:6.0
Uw Application Insights-sleutel toevoegen
Noteer uw Application Insights-instrumentatiesleutel via uw Application Insights-resource in Azure Portal.
Open
appsettings.json
uw Application Insights-instrumentatiesleutel en voeg deze toe aan deze codesectie:{ "ApplicationInsights": { "InstrumentationKey": "Your instrumentation key" } }
De Docker-installatiekopieën bouwen en uitvoeren
Controleer het Docker-bestand.
Bouw de voorbeeldafbeelding:
docker build -t profilerapp .
Voer de container uit:
docker run -d -p 8080:80 --name testapp profilerapp
De container weergeven via uw browser
U hebt twee opties om het eindpunt te bereiken:
Ga naar
http://localhost:8080/weatherforecast
uw browser.Curl gebruiken:
curl http://localhost:8080/weatherforecast
De logboeken controleren
Controleer eventueel het lokale logboek om te zien of een sessie van profilering is voltooid:
docker logs testapp
Noteer de volgende gebeurtenissen in de lokale logboeken:
Starting application insights profiler with instrumentation key: your-instrumentation key # Double check the instrumentation key
Service Profiler session started. # Profiler started.
Finished calling trace uploader. Exit code: 0 # Uploader is called with exit code 0.
Service Profiler session finished. # A profiling session is completed.
De .NET Profiler-traceringen weergeven
Wacht 2 tot 5 minuten, zodat de gebeurtenissen kunnen worden geaggregeerd naar Application Insights.
Open het deelvenster Prestaties in uw Application Insights-resource.
Nadat het traceringsproces is voltooid, wordt de knop Profiler Traces weergegeven.
Resources opschonen
Voer de volgende opdracht uit om het voorbeeldproject te stoppen:
docker rm -f testapp