8. Capture EF Core logging

Jon P Smith edited this page Sep 30, 2017 · 1 revision

8. Tools for capturing EF Core logging

It is sometimes useful for capture the logging output of EF Core, maybe to see how EF Core translates your query to SQL or how it would create your database.

I have an extension method called SetupLogging which returns a list of type List<LogOutput>, with it will add new logs output by EF Core as they occur. Here is some code that shows this in action

public void TestEfCoreLoggingExample()
    var options = SqliteInMemory.CreateOptions<EfCoreContext>();
    using (var context = new EfCoreContext(options))
        var logs = context.SetupLogging();

        var books = context.Books.ToList();

        foreach (var log in logs.ToList()) //This stops the 'bleed' problem

Note the comment "this stops the 'bleed' problem". This refers to a feature of EF Core's logger that seems to use a static or something. So, when unit tests run in parallel then you can get logs from other tests. Running a single test, or class of tests works fine.

See the class LogOutput for information on what it logs.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.