Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework core 2 SQL is always logged

After upgrading to asp.net + entity framework core 2 i noticed something weird. Before (in 1.1), i had to use DbContextOptionsBuilder.UseLoggerFactory to log SQL queries that were produced by ef, but now, i don't have to do it anymore, SQL queries are logged by default.

The logger factory is set to "Information"

loggerFactory.AddFile("Logs/MyApp-{Hour}.txt", LogLevel.Information);

Shouldn't SQL queries be associated with Debug? is it normal? is there a way to keep the logging level Information and disabling just the SQL logging?

like image 699
areller Avatar asked May 31 '26 12:05

areller


1 Answers

I found out that entity framework is supposed to log SQL in "Information" I changed the loggerFactory to

loggerFactory.AddFile("Logs/MyApp-{Hour}.txt", LogLevel.Information, new Dictionary<string, LogLevel>()
            {
                { "Microsoft", LogLevel.Error },
                { "System", LogLevel.Error }
            });

So that everything with "Microsoft" or "System" in it's namespace would log only Errors (or anything above)

like image 189
areller Avatar answered Jun 03 '26 00:06

areller