4. Detailed information: Add logging to EF Core

How do we add logging to EF Core?

Entity Framework can integrates the logging mechanism of .NET Core to log SQL and change tracking info. In this example, we will display the logs on the console. To do this, you need to install the Microsoft.Extensions.Logging.Console NuGet package.

PM> Install-Package Microsoft.Extensions.Logging.Console

To add the logging, use following code snippet in the Startup.cs of your project:

options.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))

After this implementation, you could see the logging in your console.

As you could see, EF Core will not log sensitive data, such as parameter values, by default. If you want to see your data, you could call the EnableSensitiveDataLogging() method.

options.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())).EnableSensitiveDataLogging()
.UseSqlServer(Configuration.GetConnectionString("…"))) ;

After this implementation, you will see the executed queries with parameters in your console.