Configure Application Insights in ASP.NET Core for Clear and Efficient Logging
A good logging configuration helps you troubleshoot problems quickly while avoiding unnecessary telemetry and storage costs. In ASP.NET Core, Application Insights is configured in two parts: one for connecting to Azure and another for controlling which log messages are collected.
Configure the Application Insights Connection
The ApplicationInsights section contains the connection to your Azure Application Insights resource.
{
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=...;IngestionEndpoint=..."
}
}
Using a Connection String is the recommended approach and replaces the older Instrumentation Key.
Configure Logging
The Logging section determines which messages are written by your application. In most cases, the global LogLevel settings are sufficient. If needed, you can also define provider-specific settings for Application Insights.
{
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=...;IngestionEndpoint=..."
},
"Logging": {
"LogLevel": {
"Default": "Information",
"YourCompany.YourApplication": "Information",
"Microsoft": "Warning",
"System": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"YourCompany.YourApplication": "Information",
"Microsoft": "Warning",
"System": "Warning"
}
}
}
}
What Each Setting Does
| Setting | Purpose |
|---|---|
ApplicationInsights:ConnectionString |
Connects the application to your Azure Application Insights resource. |
Logging:LogLevel |
Defines the default log levels used throughout the application. |
Logging:ApplicationInsights:LogLevel |
Optionally overrides the log levels used only by the Application Insights logging provider. |
Recommended Production Configuration
A balanced production configuration is usually:
- Default:
Information - Your application namespace:
Information - Microsoft:
Warning - System:
Warning - Microsoft.Hosting.Lifetime:
Information
For development, you can temporarily change your own application's namespace to Debug or Trace to collect more detailed diagnostic information without increasing the verbosity of framework logs.
This configuration provides useful application telemetry, keeps framework logging under control, and makes troubleshooting easier while avoiding unnecessary noise and storage costs.
Comments