How to Inspect the SQL Generated by Entity Framework Core using .NET Core's built in Logging
Tags: csharp, sql, dotnet, entityframeworkcore
Published: 2020-05-16 12:36:42

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)](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1)


### 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](https://dev-to-uploads.s3.amazonaws.com/i/psilmjdxqlh0w8rtbf3e.png)

Enable sensitive data logging by calling the EnableSensitiveDataLogging method.

```csharp 
 services.AddDbContext<DutchContext>(
                cfg =>
                {
                    cfg.UseSqlServer(_config.GetConnectionString("DutchConnectionString")).EnableSensitiveDataLogging();
                }
                );
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dfmvrihe2aluv3o4m64u.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/non5l2c6hepolh70ci44.png)

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

```csharp 
 "Microsoft.EntityFrameworkCore.Database.Command": "Information"
```

The logging config should now look something like this:

```csharp 
"Logging": {
		"LogLevel": {
			"Default": "Information",
			"Microsoft": "Warning",
			"Microsoft.EntityFrameworkCore.Database.Command": "Information"
		}
	}
```

My final configuration file:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bzcrvi6612piegeqqx6n.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/if4rngipzjc3d9p7sdjd.png)


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](https://eamonkeane.dev/how-to-view-sql-generated-by-entity-framework-core-using-logging/)*

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](https://twitter.com/eamokeane/)