DEV Community

Cover image for How to Inspect the SQL Generated by Entity Framework Core using .NET Core's built in Logging
Eamon Keane
Eamon Keane

Posted on • Updated on • Originally published at eamonkeane.dev

How to Inspect the SQL Generated by Entity Framework Core using .NET Core's built in Logging

Today I'm going to show you how to intercept and view the SQL generated by Entity Framework Core. This method takes advantage of Microsoft's built in logging for .NET core.

This is something I wish i knew about starting off. I hope it's of use to somebody else.

This method is an alternative to profiling the SQL using something like SQL Server Management Studio. This will work with an ASP.NET Core application which should cover the vast majority of use cases!

By default, .NET core outputs logs to the following locations when you call HostCreateDefaultBuilder(args) within Program.cs:

  • Console
  • Debug
  • Event Source
  • EventLog (Windows Only)

Default providers for logging is ASP.Net Core 3.1 (Microsoft Documentation)

1. Enable Sensitive Data Logging

Navigate to your startup file for your .NET Core project (Startup.cs). Now find where you've configured your application to use Entity Framework Core. This should be in the ConfigureServices method.

Alt Text

Enable sensitive data logging by calling the EnableSensitiveDataLogging method.

 services.AddDbContext<DutchContext>(
                cfg =>
                {
                    cfg.UseSqlServer(_config.GetConnectionString("DutchConnectionString")).EnableSensitiveDataLogging();
                }
                );
Enter fullscreen mode Exit fullscreen mode

Alt Text

Enabling Sensitive Data Logging allows you to view the parameters being passed into the SQL Queries in the logs.

2. Configure the Logging using your Configuration File

The next step is to tell your ASP.NET Core App to log Entity Framework Core commands.

For this step, go to your config file. Typically this is named 'Appsettings.json'. It's likely you configured the connection string for your application here.

This is what mine looks like:

Alt Text

Add a new line to "Logging" => "LogLevel"

 "Microsoft.EntityFrameworkCore.Database.Command": "Information"
Enter fullscreen mode Exit fullscreen mode

The logging config should now look something like this:

"Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.EntityFrameworkCore.Database.Command": "Information"
        }
    }
Enter fullscreen mode Exit fullscreen mode

My final configuration file:

Alt Text

That's it!

You can now run your application and see the SQL generated by Entity Framework Core in any of the default providers mentioned earlier.

Here I'm using the output window in Visual Studio 2019 and showing the output from ASP.NET Core Web Server.

Alt Text

If you can't see the output window just use 'Ctrl + Alt + O' to bring it up or find it in the View menu at the top of visual studio.

This article first appeared on Eamon Keane's Blog at www.eamonkeane.dev

Hope this helps someone out!

Let me know if it helps you out in the comments below

I'm also over on twitter where I tweet about software development. Call over and say hello! @eamokeane

Top comments (17)

Collapse
 
amsmart profile image
Emmanuel Oluwagbemiga Adebiyi (Smart)

Awesome tutorial

Collapse
 
galwaycoder profile image
Eamon Keane

glad it helped you!

Collapse
 
toastedguy2 profile image
ToastedGuy2

Thanks mate. Simple and Consice

Collapse
 
galwaycoder profile image
Eamon Keane

I'm glad you found it helpful!

Collapse
 
krasni profile image
krasni

Thank you very much, it works like a charm!

Collapse
 
galwaycoder profile image
Eamon Keane

You're very welcome

Collapse
 
jon001 profile image
jon001

Oh my word that is lovely. You've brightened my day, Eamon!

Collapse
 
galwaycoder profile image
Eamon Keane

Glad it helped!

Collapse
 
izakntun profile image
Isaac CantΓΊn

Great tutorial!! very useful! Thank You

Collapse
 
galwaycoder profile image
Eamon Keane

welcome! Glad it helped

Collapse
 
vasiliyholub1 profile image
vasiliyholub1

Thanks. Work from a first time!

Collapse
 
galwaycoder profile image
Eamon Keane

great stuff! Welcome

Collapse
 
night_sampa profile image
Luciano Oiticica Lemgruber

Thanks.

Collapse
 
riccardo1993 profile image
riccardo1993

Thx, it is what I needed!

Collapse
 
galwaycoder profile image
Eamon Keane

great !

Collapse
 
offirpeer profile image
Offir

This is not working for me, but I use .netcore 2.x

Collapse
 
galwaycoder profile image
Eamon Keane

hey, That might be the issue alright. Did you get it resolved in the end?