Azure Function Application Insights Logging not Honoring Host.json Configuration

1k Views Asked by At

I have enabled Application insights for a function app and traces are being logged successfully, however currently messages from all log levels are being included in the Application insights traces table. I have configured my host.json like so, which according to the docs should log "Information" and above for Function, Trace and above for Host.Results, and nothing else.

{
 "version": "2.0",
 "logging": {
  "logLevel": {
    "default": "None",
    "Host.Results": "None",
    "Function": "Information",
    "Host.Aggregator": "Trace"
  },
  "applicationInsights": {
    "samplingSettings": {
      "isEnabled": true,
      "excludedTypes": "Dependency;Request;PageView",
      "maxTelemetryItemsPerSecond" : 20,
      "httpAutoCollectionOptions": {
        "enableHttpTriggerExtendedInfoCollection": true,
        "enableW3CDistributedTracing": false,
        "enableResponseHeaderInjection": false
      }
    }
  }
},
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "functionTimeout": "02:00:00",
   "extensions": {
   "queues": {
            "maxPollingInterval": "00:00:01",
            "visibilityTimeout" : "00:00:15",
            "batchSize": 1,
            "newBatchThreshold": 2,
            "maxDequeueCount": 2
        }
    }
}

I have also tested with the following logLevel value which should prevent any traces from being logged

"logLevel": {
    "default": "None",
  },

Finally, I have attempted to define the logLevel inside the application insights subsection as well as inside the logging section, like recommended here, but this also has no effect and does not seem to be a valid config per the host.json reference.

Modifying the logLevel section does correctly change the console log level when running the function locally, but has no effect on what is logged in Application insights. I would greatly appreciate any help with this issue. We are unable to use Application insights at the moment because the quantity of data being logged is too great and it is driving our costs up.

Edit: Added complete host.json

2

There are 2 best solutions below

0
On

I have created a function app and integrated application insights to it.

  • Then I have created a Timmer trigger function app in my local and connected to application insight through adding instrumentation key.

  • Iam able to run the function app and tracing the logs in my insight but, Iam getting all the default & function logs in same trace table only. So, to overcome from that I have taken log levels please check the below file.

host.json :

{
    "version": "2.0",
    "logging": {
      "logLevel": {
        "default": "Information",
        "Host.Results": "Information",
        "Function": "Information",
        "Host.Aggregator": "Trace",
        "Function.Function1.User": "Trace",
        "Function.Function1.System": "Debug"

      },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Dependency;Request;PageView",
        "maxTelemetryItemsPerSecond": 20,
        "httpAutoCollectionOptions": {
          "enableHttpTriggerExtendedInfoCollection": true,
          "enableW3CDistributedTracing": false,
          "enableResponseHeaderInjection": false
        }
      }
    }
  },
  "functionTimeout": "02:00:00",
  "extensions": {
    "queues": {
      "maxPollingInterval": "00:00:01",
      "visibilityTimeout": "00:00:15",
      "batchSize": 1,
      "newBatchThreshold": 2,
      "maxDequeueCount": 2
    }
  }
}
  • Below is my sample function app code.
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */1 * * * *")] TimerInfo timer, ILogger logger)
    {

        logger.LogInformation("This is an information-level log message.");
        logger.LogWarning("This is a warning-level log message.");
        logger.LogError("This is an error-level log message.");
        logger.LogTrace("This is a trace-level log message.");
        logger.LogDebug("This is a debug-level log message.");
        logger.LogCritical("This is a critical-level log message.");
    }
}
  • Iam able to run successfully by communicating with host.json file.

enter image description here enter image description here

  • Iam able to see the function logs with separation by security levels

Query :

traces
| where severityLevel in (0, 1, 2, 3, 4, 5)
| order by timestamp desc

Results : enter image description here enter image description here enter image description here

0
On

According to the ms doc in isolated Azure functions, you need to remove default Application insights filters. Then you will be able to configure logging level by host.json

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(s =>
    {
        s.AddApplicationInsightsTelemetryWorkerService();
        s.ConfigureFunctionsApplicationInsights();
        s.Configure<LoggerFilterOptions>(options =>
        {
            // The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
            // Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
            LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

            if (toRemove is not null)
            {
                options.Rules.Remove(toRemove);
            }
        });
    })
    .Build();