Manually creating a DbContext and enable logging
In an application I have working with a needed to manually create a DbContext
.
But doing so, disabled all logging from the DbContext
.
The problem
In most cases a DbContext
is initialized via dependency injection. But I
needed to customize which connection string to use. I did it like this:
This works just fine. But a subtle change is that the DbContext
will not be
able to log anything.
The solution
It is not hard to fix just. Just make sure that the logging factory is setup via
DbContextOptionsBuilder
by calling UseLoggerFactory
:
Summary
I found this the hard way when I wanted to see exact which SQL statements were executed. I had enabled logging in my configuration file:
But still nothing was output. It worked for other DbContext
which made me
confused. Looking back, it was quite obvious. But it took a while for me for
figure this out.