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

非 Y 不嫁゛ 提交于 2020-12-13 03:32:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/65034967/avoid-severity-level-0-logging-in-application-insights-from-function-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!