I have a very simple Asp .Net API project where I am trying to add some trace logging. I need some help with Application Insights configuration for that logging. Right now its capturing ALL traces while in appsettings.json I have set the Default level to Information.
I have this code in my Program.cs
services.AddApplicationInsightsTelemetry();
Followed by:
Trace.Listeners.Add(new Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener());
And the following in my appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning"
}
}
I have these two packages in my .csproj file:
<PackageReference Include="Microsoft.ApplicationInsights.TraceListener" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
I am expecting that only traces with level Information or above should get logged to Application Insights however I am also seeing every thing getting logged to Application Insights. What do I need to do change the trace logging level globally?
I see this Verbose messages in my AI instance logged right now with the above config.
If Information level is kept , then it logs all the logs which are at information level and above. I have followed Microsoft-document and also SO-thread's answer provided by @Harshitha:
I have used below code in program.cs:
and added below lines in csproj:
Added logger code in controller:
appsettings.json:
Only logs of information has been logged not trace level and all:
If you get other information use a level higher which only needs to be logged so that unwanted logging does not happen.