Entityframework.docs: Support logging on DB level

Created on 28 Jul 2020  路  6Comments  路  Source: dotnet/EntityFramework.Docs

I'm migration from EF to EF Core 5.0 (preview 6 now), but there is no Log in DbContext.Database anymore.

using (MyDBContext db = ContextFactory.GetMyContext())
{
    db.Database.Log = logInfo => FileLogger.Log(logInfo);
}
area-context-management closed-fixed

Most helpful comment

Could be a useful addition to the docs.

All 6 comments

The API linked to above has been introduced for 5.0, which hasn't yet been released. Until that happens, you can still configure logging in EF Core as detailed in this page.

Ok, but this change logger for all entire DBContext.
In project which I am upgrading to EF Core there is only one call to DbContext.Database.Log - in only one controller endpoint.
How to achieve this in EF Core 5.0 preview 6?

@Saibamen You could setup a switch on the context to allow logging to be switched on when needed. For example:

```C#
public class SomeDbContext : DbContext
{
public bool LoggingEnabled { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .LogTo(s =>
        {
            if (LoggingEnabled)
            {
                Console.WriteLine(s);
            }
        })
        .EnableSensitiveDataLogging()
        .UseSqlServer(Your.SqlServerConnectionString);

}
```

Could be a useful addition to the docs.

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VirMaker picture VirMaker  路  4Comments

mjehle82 picture mjehle82  路  3Comments

MohammadMQ picture MohammadMQ  路  3Comments

speciesunknown picture speciesunknown  路  3Comments

SychevIgor picture SychevIgor  路  4Comments