zzzprojects / EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
https://entityframework-classic.net
Other
103 stars 27 forks source link

Logging via Microsoft.Extensions.Logging? #31

Closed schmitch closed 5 years ago

schmitch commented 5 years ago

Currently it would be great if EF-Classic would support logging via Microsoft.Extensions.Logging.. It took me a while to figure out that the query was really executed, since I had no visible logging, only my ef core logs would've been posted, so it first seemed that EF-Classic did not work.

actually for Z.EntityFramework.Plus.EFCore it should be relativ simple by using GetService<ILogger<Microsoft.EntityFrameworkCore.Database.Command>> however would probably be problematic for ef5/ef6

JonathanMagnan commented 5 years ago

Hello @schmitch ,

I don't think that's something we would like to implement by default. We try to keep being as close as EF6 which already have somewhat his own database log: https://www.entityframeworktutorial.net/entityframework6/database-command-logging.aspx

schmitch commented 5 years ago

sadly that is not possible in efcore. the only way would be to add a logger to the database driver in my case npgsql: https://www.npgsql.org/doc/logging.html

Logging in efcore is done via the LoggingFactory which EF-Classic does not implement: https://www.entityframeworktutorial.net/efcore/logging-in-entityframework-core.aspx

logging on the npgsql level would mean that both efcore and pgsql logs would be duplicated.

JonathanMagnan commented 5 years ago

sadly that is not possible in efcore.

EF Classic is not an extension of EF Core and is it expected to don't have the same behavior. It's a new whole Entity Framework version.

Are we speaking of the same library? In your text, you speak of EF Classic but also of EF Plus for EF Core.

schmitch commented 5 years ago

well EF Classic for dotnet core (efcore) is not loggable like it was done in entityframework5/6 you can't attach a Console.WriteLine to DbContext.Database.Log. Thus it's impossible to log statements in EF Classic, the only way to log is to actually log at the driver level.

JonathanMagnan commented 5 years ago

Hello @schmitch ,

I'm sorry, I still don't understand this request.

It seems we have no problem on our side to use Console.WriteLine with the log event.

What are we missing?

public class EntityContext : DbContext
{
    public EntityContext() : base(My.ConnectionString)
    {
        this.Database.Log += Console.WriteLine;
    }

    // ...code...
}