Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Entity Framework logging in Azure Function

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?

like image 689
Andrey Stukalin Avatar asked Mar 08 '26 20:03

Andrey Stukalin


2 Answers

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.

like image 113
Andrey Stukalin Avatar answered Mar 11 '26 16:03

Andrey Stukalin


This worked for me:

builder.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.None);
like image 41
andy Avatar answered Mar 11 '26 14:03

andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!