Simple Logging API for database interactions (similar to Database.Log)
ajcvickers opened this issue · 12 comments
This would make use of the core logging services without the need to do any wire-up.
We may want a Database.Log
for SQL and then something like Context.Log
for everything.
Is there a simpler way for logging (e.g. to console for debugging) other than the example described in #2795 by now?
Feel free to reuse my naive extension method here: https://github.com/ErikEJ/EntityFramework7.SqlServerCompact/blob/master/src/Provider40/Extensions/SqlCeDbContextExtensions.cs and here: https://github.com/ErikEJ/EntityFramework7.SqlServerCompact/tree/master/src/Provider40/Extensions/Logging
@leak - not yet, you still need to plug all the low level stuff together
BTW, just logging everything is relatively simple:
db.GetService<ILoggerFactory>().AddProvider(new ConsoleLoggerProvider());
@bricelam that needs to go in app startup right since the ServiceProvider is shared between context instances? Otherwise you add it multiple times.
Hmm yes, I think you're right...
FYI http://docs.efproject.net/en/latest/miscellaneous/logging.html shows how to achieve the same thing before we have full support for this feature
Need to re-triage this. The state of logging right now means that many people will have to use an obsolete API in logging to get logs from EF. We should decide whether to try to do something here for RTM.
Note: #8350 has been moved to the backlog. It should be re-triaged after this work is done.
Note: See #12183. Make sure that simple logging it attached before the internal service provider has been built.
Having fallen foul of of using the obsolete logging API (#12183) can I give feedback on the two use cases where getting access to logs for a specific DbContext. Or alternatively being able to identify logs for a specific DbContext out of all the logs.
- Unit Testing: I often want to output the EF logs in a unit test. It is hard to check the quality of the SQL produced in the main application, but by outputting the SQL when unit testing I can manually check that the SQL makes sense.
- Using multiple DbContexts: I am a fan of DDD and having multiple DbContexts to cover subsets of the database relevant to different parts of my application ("bounded contexts") - all my libraries support this and I am working on an application which uses this. The problem comes when trying to work out what SQL was produced by which DbContext when inspecting the logs.
You may have a solution to these that I have missed. If so please let me know.