mattb
mattb

Reputation: 472

Azure app service webjob, how to disable trace logs to application insights

I have a .net framework webjob which reads the APPINSIGHTS_INSTRUMENTATIONKEY env setting and via the ILogger is sending all debugging statements to application insights as trace logs.

This is getting quite expensive and these logs are not needed.

Is there a way of disabling the trace logs being sent to app insights?

I've tried using appsettings.json, applicationinsights.config but nothing seems to take effect.

Edit: Have also tried adding env settings like Logging__LogLevel__Default = None as suggested here: https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#set-log-level-by-command-line-environment-variables-and-other-configuration but made no difference

Upvotes: 0

Views: 746

Answers (1)

Pravallika KV
Pravallika KV

Reputation: 8400

These logs are coming from ILogger logs. You can use a filter to not collect ILogger logs, so that these logs won't be sent to ApplicationInsights.

enter image description here

The below code can be used to disable ILogger logs from all category completely.

builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.None);

Code Snippet:

 static async Task Main()
        {
            var builder = new HostBuilder();
            builder.UseEnvironment(EnvironmentName.Development);
            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();
                string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                if (!string.IsNullOrEmpty(instrumentationKey))
                {
                    b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
                }

            });
builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.None); 
           
            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddAzureStorageQueues();
            });
            var host = builder.Build();
            using (host)
            {
                await host.RunAsync();
            }

        }
       

Applications.json:

you can also configure in application settings as shown below:

"Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "None",
        "Microsoft.Hosting.Lifetime": "None"
      }

Upvotes: 0

Related Questions