azure-functionsazure-application-insights

Basic Azure function logging isn't working


I'm trying to setup basic Application Insights logging based on host.json in a .net 8.0 isolated Azure function project. Below you can find the file I'm using.

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "debugOnly",
    "logLevel": {
      "Host.Aggregator": "Trace",
      "Host.Results": "Trace",
      "Function": "Trace",
      "default": "Trace"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    }
  }
}

I'm trying to activate AI logging in the builder code like this :

.ConfigureServices(services =>
{
    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
    // You will need extra configuration because above will only log per default Warning (default AI configuration) and above because of following line:
    // https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs#L427
    // This is documented here:
    // https://github.com/microsoft/ApplicationInsights-dotnet/issues/2610#issuecomment-1316672650
    // So remove the default logger rule (warning and above). This will result that the default will be Information.
    services.Configure<LoggerFilterOptions>(options =>
    {
        var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
            == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

        if (toRemove is not null)
        {
            options.Rules.Remove(toRemove);
        }
        //options.MinLevel = LogLevel.Trace;
    });

When I look at the ILogger that is passed along in the azure function class constructor, it has :

At least I would expect a MinLevel = Trace ... Am I missing something in my host.json file?

If I add options.MinLevel = LogLevel.Trace to my builder code, I do get MinLevel = Trace. But that's not the purpose, for our UAT & PROD environments we would like to set MinLevel = Information

Been looking around for post with similar approach but couldn't find it.

Regards, Sven


Solution

  • Unfortunately, you need to set the MinLevel using program.cs only as I believe ConfigureFunctionsWorkerDefaults/ConfigureFunctionsWebApplication sets the MinLevel to Information.

    Overriding the ILogger Minlevel is not feasible using host.json currently and also github issue is open for the same.

    You can either use the same code which you are using currently or use the below code.

    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddConsole(); 
            logging.SetMinimumLevel(LogLevel.Trace);
        })
        .Build();
    
    host.Run();
    

    This will set the Minlevel to Trace.

    enter image description here

    You can also add the conditions based on the environments in programs.cs file.