Logging EF Core

To monitor the interaction between EF Core and the database, we can enable logging. This requires the following:

  • The registering of a logging provider
  • The implementation of a logger

In Visual Studio 2017 or Visual Studio Code, add a class file to your project named ConsoleLogger.cs.

Modify the file to define two classes, one to implement ILoggerProvider and one to implement ILogger, as shown in the following code, and note the following:

  • ConsoleLoggerProvider returns an instance of ConsoleLogger. It does not need any unmanaged resources, so the Dispose method does not do anything, but it must exist.
  • ConsoleLogger is disabled for log levels None, Trace, and Information. It is enabled for all other log levels.
  • ConsoleLogger ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.