Warm tip: This article is reproduced from serverfault.com, please click

Avoid severity-level-0 logging in application insights from function app

发布于 2020-11-27 09:35:05

In my azure solution, I have 1 app service and 2 function apps logging to 1 application insights instance. In a specific environment I want to reduce the logging load, so I wanted to get rid of severity-level-0 logs.

I am currently focusing on one of the function apps, let's call it fa1. The logging statements I add with ILogger as LogDebug do not show up in application insights, as expected. However I can see the following entries in application insights:

  • "Poll for function '{name of function in fa1}' on queue '{...}' with ClientRequestId '{...}' found 0 messages in 5 ms."
  • "Function '{name of function in fa1}' will wait 60000 ms before polling queue '{...}'."

I also see the following entries, but I do not know which service is generating them:

  • "[HostMonitor] Host process CPU stats: EffectiveCores=1, CpuLoadHistory=(0,0,0,0,0,0,0,0,0,0), AvgCpuLoad=0, MaxCpuLoad=0"
  • "[HostMonitor] Host aggregate CPU load 0"

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "Function": "Warning",
      "default": "Warning"
    }
  }
}

Startup.Configure():

builder.Services.AddLogging(loggingBuilder =>
{
    var key = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
    loggingBuilder.AddApplicationInsights(key);
});
builder.Services.AddSingleton(sp => // Needed for injected ILogger<> to log in AI
{
    var key = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
    return new TelemetryConfiguration(key);
});

I tried loggingBuilder.AddApplicationInsights(key).SetMinimumLevel(LogLevel.Warning); as well. None of the settings worked, the logging entries mentioned above kept appearing.

Note that I am setting Warning just for testing. At the end I want Information.

What can I do to get rid of those severity-level-0 logging entries?

Questioner
JoaoRibeiro
Viewed
11
Peter Bons 2020-11-30 16:55:22

Take a look at the different log categories here.

I'd say to reduce logging but keep important information like requests and dependencies set the Microsoft, Worker and Function.<YOUR_FUNCTION_NAME> categories to LogLevel warning (or none), but keep Function.<YOUR_FUNCTION_NAME>.User and all the others at information. This will remove most logging but keep statistics and the success/failure logs available in the portal.

By doing loggingBuilder.AddApplicationInsights(key).SetMinimumLevel(LogLevel.Warning); you are only changing the loglevel for your the Function category, not the others.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "Function.<YOUR_FUNCTION_NAME>": "Warning",
      "Function.<YOUR_FUNCTION_NAME>.User": "Information",
      "Microsoft": "Warning",
      "Worker": "Warning",
      "default": "Information"
    }
  }
}

The category Function.<YOUR_FUNCTION_NAME> is a tricky one. It generates both useful logs and some noise. Try both Warning and Information as log level and see what best works for you.

EDIT The way you configure the function is wrong. Application Insights is added by Azure Functions automatically (source). If you want to directly use a TelemetryClient you need to add the NuGet package Microsoft.Azure.WebJobs.Logging.ApplicationInsights and that is it.

The default integration will pick up the configuration from the host.json file. Right now you are setting up the logging manually which will ignore the settings since the configuration is not loaded. That is why, for example, the message Poll for function '{name of function in fa1}' on queue '{...}' with ClientRequestId '{...}' found 0 messages in 5 ms. that belongs to category Microsoft.Azure.WebJobs.Host.Queues.Listeners.QueueListener is logged and not filtered. So remove the code you have shown in the question.