Azure Functions - Logging not working correctly in local

Singh, Rahul 1 Reputation point
2024-04-17T14:04:02.69+00:00

I have a simple dotnet-isolated Azure function written in .Net 8 which has 1 Http trigger function. I have another custom database logger which is added to this function to enable database logging.

When I am executing the function, none of the Microsoft related logging are getting filtered and it's logging hundreds of entries in my database. I have already tested my custom logger with a simple console application where the logging filtering works correctly. What's interesteing is apart from my own custom logger, the console logs are also not getting filtered.

This is how my Program.cs looks:-

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureAppConfiguration((context, builder) =>
    {
        builder.SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("local.settings.json", optional: true);
    })
           .ConfigureLogging((builder, logging) =>
               {
                   logging.AddConsole();
                   logging.AddDbLogger(options =>
                     {
                       builder.Configuration.GetSection("Database")
                              .GetSection("Options").Bind(options);
                           });
                       })
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();

I have added the resp. logging in host.json file:-

{
    "version": "2.0",
    "logging": {
      "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true,
          "excludedTypes": "Request"
        },
        "enableLiveMetricsFilters": true
      },
      "logLevel": {
        "default": "Information",
        "Microsoft": "Error"
      },
      "Database": {
        "logLevel": {
          "default": "Error"
        }
      },
      "console": {
        "logLevel": {
          "default": "Error"
        }
      }
    }
}

Still it shows Log entries in Console and Database.

enter image description here

User's image

Is there anything wrong with my host.json?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,281 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,264 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,821 Reputation points Microsoft Employee
    2024-04-17T23:58:55.8+00:00

    Hey @Singh, Rahul

    When it comes to logging for dotnet-isolated, have a look at Managing log levels in under Guide for running C# Azure Functions in an isolated worker process | Microsoft Learn. Log levels are separate between Application Insights and the host.What I suggest doing is setting the default to warning or error to prevent excessive logging and adjust logging for Host.

    {
      "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "default": "Warning",
          "Host.Aggregator": "Trace",
          "Host.Results": "Information",
          "Function": "Information"
        }
      }
    }
    

    You can also look at filtering specific namespaces that you're not interested in seeing logged.

        .ConfigureLogging(logging =>
        {
            // Disable IHttpClientFactory Informational logs. 
            logging.AddFilter("System.Net.Http.HttpClient", LogLevel.Warning);
        })
    

    You can also remove message handlers, see https://github.com/aspnet/HttpClientFactory/issues/196#issuecomment-432755765.