Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable raw SQL logging in Entity Framework Core

How do I enable the logging of DbCommand raw SQL queries?

I have added the following code to my Startup.cs file, but do not see any log entries from the Entity Framework Core.

void ConfigureServices(IServiceCollection services)
{
    services.AddLogging();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug(LogLevel.Debug);
}

I'm expecting to see something like this:

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilder...
SELECT [t].[Id], [t].[DateCreated], [t].[Name], [t].[UserName]
FROM [Trips] AS [t]
like image 856
codersl Avatar asked Sep 02 '16 01:09

codersl


1 Answers

From MVC Core 2, logging SQL is the default behaviour. Just make sure logging level in appSettings json file is correct.

"Logging": {
  "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information"
  }
}
like image 200
Sharon Zhou Avatar answered Oct 06 '22 04:10

Sharon Zhou