Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core context initialized logs with every command #2436

Closed
kewur opened this issue Jul 16, 2022 · 2 comments
Closed

EF Core context initialized logs with every command #2436

kewur opened this issue Jul 16, 2022 · 2 comments

Comments

@kewur
Copy link

kewur commented Jul 16, 2022

Hi, I've been using npsql with EF core for a while now, while running locally (non prod environemnt), we get spammed with

Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 6.0.2 initialized 'MyContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL:6.0.3+94d0a8eaeb9cf5af6ce55a9e59153c1a1be3244f' with options: SensitiveDataLoggingEnabled using snake-case naming  (culture=)

on all code paths that accesses the db context. Having trouble understanding/deep diving into this issue as I don't know why the context keeps getting initialized every single time we access it. Is this expected behavior? this is how we are registering our context,

what steps can we take to understand what's causing this? Could this be happening because we're registering the context with AddScoped? Or is this just expected behavior?

        services.AddDbContext<MyContext>(x => ConfigureDbContext(x, awsRegion, isDevelopment, loggerFactory));
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        services.AddScoped<IMyContext>(provider => provider.GetService<MyContext>());

           private static void ConfigureDbContext(DbContextOptionsBuilder options,
                                           RegionEndpoint awsRegion,
                                           bool isDevelopment,
                                           ILoggerFactory loggerFactory)
    {
        string connectionString = RDSConnectionStringFactory.GetRDSConnectionString(awsRegion);

#if DEBUG || TEST
        options.UseLoggerFactory(loggerFactory); // entity framework logging remove when not debugging. EF logging ef core logging.
#endif

        try
        {
            options.UseNpgsql(connectionString, x =>
                                                {
                                                    x.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10),
                                                                           new List<string>());
                                                })
                   .UseSnakeCaseNamingConvention();

            if (isDevelopment)
            {
                options.EnableSensitiveDataLogging();
            }
        }
        catch (Exception exception)
        {
            Console.Write(exception.Message);
        }
    }

Further technical details

Npgsql version: 6.0.3
PostgreSQL version: 12.4
Operating system: Ubuntu 22

@roji
Copy link
Member

roji commented Jul 19, 2022

This is the expected behavior; AddDbContext creates a new context for each web request (that's what Scoped means), and each new context that gets initialized makes EF log that message. This isn't indicative of a problem or anything, though I've opened dotnet/efcore#28480 to consider downgrading that message to be Debug-level rather than Information.

Note that you can use context pooling to recycle context instances, which may improve perf a bit and would make most of those messages go away (once a sufficient number of contexts is initialized and pooled).

I'm going to go ahead and close this as by-design, but feel free to post back here.

PS general EF Core issues (like this one) belong in the EF Core repo (https://github.com/dotnet/efcore), whereas issues on the Npgsql PostgreSQL EF provider belong in https://github.com/npgsql/efcore.pg. This repo is for the low-level SQL driver.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2022
@roji roji transferred this issue from npgsql/npgsql Jul 19, 2022
@Tawqi3i
Copy link

Tawqi3i commented Dec 24, 2022

This does the trick in .net6:
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) // this is the one that hides it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants