Azure Functions - Logging not working correctly in local

Singh, Rahul 6 Reputation points
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.
5,107 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.
11,011 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ryan Hill 28,631 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.


  2. Pinaki Ghatak 4,610 Reputation points Microsoft Employee
    2024-05-20T08:46:18.9933333+00:00

    Based on the provided information, it seems that the logging configuration in the host.json file is not working as expected. Here are a few things you can check:

    1. Make sure that the host.json file is in the root directory of your function app. If it is not in the root directory, the logging configuration may not be applied.
    2. Check the syntax of your host.json file. The logLevel property should be an object that contains the logging levels for different categories. In your host.json file, the default, Microsoft, Database, and console properties should be enclosed in double quotes. Here is an example of a corrected 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"
                    }
                }
            }
        }
    }
    
    1. Check the logging configuration of your custom database logger. Make sure that it is not overriding the logging configuration in the host.json file.
    2. Check the logging configuration of your console logger. Make sure that it is not overriding the logging configuration in the host.json file. If none of these solutions work, it is possible that there is a bug in the Azure Functions runtime. In this case, you may want to contact Microsoft support for further assistance.

    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.