Application Insights capturing ALL traces while appsettings.json level is set as Inforamtion

56 Views Asked by At

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.

1

There are 1 best solutions below

1
RithwikBojja On

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:

builder.Services.AddSingleton(typeof(ITelemetryChannel), new ServerTelemetryChannel());
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddServiceProfiler();

and added below lines in csproj:

<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.4.0" />

Added logger code in controller:

 _logger.LogInformation("Hello Rithwik");
 _logger.LogTrace("Hello");

enter image description here

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "InstrumentationKey": "a2dec36e"
  }
  }

Only logs of information has been logged not trace level and all:

enter image description here

If you get other information use a level higher which only needs to be logged so that unwanted logging does not happen.