Home > Software engineering >  How to get the Log property of DatabaseFacade?
How to get the Log property of DatabaseFacade?

Time:12-09

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):

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!

CodePudding user response:

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));
  • Related