12

I have an Azure Function in .net5 (a.k.a. dotnet-isolated) and I've added Entity Framework like this

services.AddDbContext<EfDbContext>(options =>
{
    options.UseSqlServer(context.Configuration[...], builder =>
    {
        builder.EnableRetryOnFailure();
    });
});

When I run the function I see the DB queries from EF in the console (info level judging by the color).

enter image description here

How can I disable them? I tried obvious options in my host.json:

"logging": {
    "logLevel": {
        "Microsoft.EntityFrameworkCore": "Warning",
    }
}

and

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

or even

"logging": {
    "logLevel": {
        "Microsoft": "Warning",
    }
}

but it didn't help. The only option which worked was

"logging": {
    "logLevel": {
        "default": "Warning",
    }
}

Am I missing something?

1
  • Could it be that you have custom logging? I would try to search for 'Executed DbCommand' in the project Commented May 13, 2021 at 11:49

2 Answers 2

18

Found a way. That's a bit strange though and I still wanna understand how it works and why it's done this way. Anyways, from AppInsights logs I found out that EF logs are written under the Function.<YOUR_FUNCTION_NAME>.User category. (turns out that a standard EF category is somehow overwritten by the functions runtime or so?).

That means that I can impact on the overall log level of a specific function by

    "logging": {
        "logLevel": {
            "Function.MyFunc1.User": "Debug",
            "Function.MyFunc2.User": "Warning"
        }
    }

in the host.json. It can be helpful but it doesn't solve my problem.

If however I now add a filter in the Program.cs like this:

var host = new HostBuilder()
  .ConfigureFunctionsWorkerDefaults()
  ...               
  .ConfigureLogging(builder =>
  {
    builder.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.Warning);
  })
  ...

the EF info logs are gone.

Sign up to request clarification or add additional context in comments.

2 Comments

just to possibly save someone`s time you need to reference using Microsoft.Extensions.Logging , my intellisense in VS2022 was not giving me this suggestion only displaying that AddFilter does not exist
Have you tried,: builder.Logging.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.Warning);
2

This worked for me:

builder.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.None);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.