Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the Log property of DatabaseFacade?

When I googled for "entityframework" and "logging", this article popped up and several people from here have also mentioned the same article.

However, when I tried it, I can’t seem to get the Log property. What am I missing?

My implementation of the DbContext (btw, this was generated by Entityframework’s own scaffolding):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

internal partial class SharedContext : DbContext
{
    public SharedContext()
    {
    }...
}

Here’s how I tried to use:
SharedContext context = new();

//I am getting CS1061 (DatabaseFacade does not contain a definition for Log....
context.Database.Log = Console.Write;  

Please help. Thanks!

>Solution :

Your question is tagged with .NET 6 and EF Core while the article refers to EF 6 which is previous iteration of EF for .NET Framework. You should look into logging documentation for EF Core. For example using simple logging via overloading OnConfiguring method:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.LogTo(Console.WriteLine);

Also probably you should consider setting up context via dependency injection (DbContext Lifetime, Configuration, and Initialization):

services.AddDbContext<SharedContext>(opts => opts // or AddDbContextFactory
      .Use{YourDatabaseType}(...)
      .LogTo(Console.WriteLine));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading