I had configured my ASP.NET Core application to use Application Insights for logging. However when I took a look at the logged messages inside Application Insights, nothing was logged? What did I do wrong?
The only code I added to enable logging was this:
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddApplicationInsightsTelemetry(); |
But I couldn’t see what could be wrong in this one line…
I searched through the documentation to find a solution. There I noticed the following sentence:
By default, ASP.NET Core applications have an Application Insights logging provider registered when they're configured through the code or codeless approach. The registered provider is configured to automatically capture log events with a severity of LogLevel.Warning or greater.
Aha! By default the ApplicationInsightsLoggerProvider
will only log messages with LogLevel Warning or higher.
Let’s change this to also log Informational messages.
There are 2 ways to do this;
- Through configuration
- Through code
Change LogLevel through configuration
To change the log level through configuration, we need to update our appsettings.json
and add an ApplicationInsights section inside our Logging configuration section:
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Warning" | |
}, | |
"ApplicationInsights": { | |
"LogLevel": { | |
"Default": "Information" | |
"Microsoft": "Warning" | |
} | |
} | |
} | |
} |
Change LogLevel through code
A second option is to change the log level through code. This can be done through the AddFilter<>
method:
builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information) | |
builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning) |