Make these necessary changes to the app.
- Install client library
dotnet add package Microsoft.ApplicationInsights.AspNetCore
- Configure the app to use Azure Monitor
An ASP.NET Core app typically has a Program.cs file that "builds" the app. Find this file and apply these changes.
- Add the following usings at the top:
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;- Before calling
builder.Build(), add these lines:
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.ConfigureOpenTelemetryTracerProvider(t => {
t.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.AddSource("MassTransit");
});
- If Serilog is used in the app, install the following client library:dotnet add package Serilog.Sinks.ApplicationInsights
Add the following using at the top:
```csharp
using Serilog.Sinks.ApplicationInsights;
And add Application Insights as a sink to Serilog configuration:
.WriteTo.ApplicationInsights(TelemetryConverter.Traces,LogEventLevel.Information)The App Insights resource has a connection string. Add the connection string to the appsettings.json file. You can use Azure CLI to query the connection string of the App Insights resource. See scripts/appinsights.ps1 for what Azure CLI command to execute for querying the connection string.
After getting the connection string, add it to the appsettings.json file with its value.
"ApplicationInsights": {
"ConnectionString":"{your_application_insights_connection_string}"
}