Suppress function app's insight logs for EntityFrameWorkCore

Aqil NNIT AQLA 0 Reputation points
2025-02-24T15:12:07.54+00:00

Has anyone tried suppressing information logs generated by Microsoft.EntityFrameworkCore.Database.Command dynamically using environment variables?

When using EntityFramework in function app, it logs by default ALL its SQL statements to Application Insights. This takes up a LOT of space in insights, and we already have problems with it hitting the max (and then not logging anymore)

Function Apps provide a capability, where adding a new EnvironmentVariable with a specially crafted name and as value the 'Minimum' loglevel, you can filter out these traces from App Insights logging dynamically. But it's not working.

-
Environment variables used: -AzureFunctionsJobHost__logging__applicationInsights__logLevel__Microsoft.EntityFrameworkCore.Database.Command -> Set to “Warning” -Logging__ApplicationInsights__LogLevel__Microsoft.EntityFrameworkCore.Database.Command ->Set to Warning None of the above Env variables worked

  • Secondly, Host.Json. with reference to our last call, entries were given at level levels having 'Microsoft.EntityFrameworkCore.Database.Command': 'Warning' but unfortunately none worked.
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,473 questions
{count} votes

Accepted answer
  1. Khadeer Ali 3,675 Reputation points Microsoft Vendor
    2025-02-25T14:44:20.43+00:00

    @Aqil NNIT AQLA ,

    Thanks for sharing your host.json logging info. We noticed you have two logLevel sections, which might be causing the problem.

    Could you try removing this part:

    "logLevel": {
      "default": "Information",
      "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
    }
    
    

    So your logging section looks like this:

    "logging": {
      "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true,
          "excludedTypes": "Request;Exception"
        },
        "enableLiveMetricsFilters": true,
        "logLevel": {
          "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
        }
      }
    }
    
    

    Then, restart your function and see if the SQL logs are reduced.
    Additionally, to further reduce the amount of data sent to Application Insights, you can adjust the sampling settings. For example, you can set a sampling percentage to only collect a fraction of the telemetry. This can be done by adding a percentage property to the samplingSettings

    Regarding your question about dynamic configuration:

    • You are correct that programmatic filtering (loggingBuilder.AddFilter) is a permanent code change.
    • The host.json file is the most reliable configuration based approach.
    • While environment variables should work, there are some inconsistencies with how the Azure Functions host handles them, especially when combined with Application Insights. The Host.json file is the most reliable configuration method.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.