linq2db.EntityFrameworkCore icon indicating copy to clipboard operation
linq2db.EntityFrameworkCore copied to clipboard

Improve/document query logging setup

Open MrDave1999 opened this issue 2 years ago • 10 comments

How can I visualize the generated SQL code? It is not displayed in the Visual Studio output box.

MrDave1999 avatar Sep 01 '22 18:09 MrDave1999

We use EF.Core logging insfrastructure, so you just need to configure ef.core logging https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/

MaceWindu avatar Sep 02 '22 09:09 MaceWindu

Closing as answered

MaceWindu avatar Feb 26 '23 14:02 MaceWindu

@MaceWindu Sorry for the late reply. I have not been able to solve the problem. EF Core is supposed to fully integrate with Microsoft.Extensions.Logging when I call the AddDbContext method but still does not work when I call linq2db's UpdateAsync method. The sql code is not displayed.

MrDave1999 avatar Oct 06 '23 15:10 MrDave1999

@MrDave1999 you probably need to enable logging on linq2db side, check this. Otherwise provide your logging configuration code so we can examine what is missing.

MaceWindu avatar Oct 07 '23 08:10 MaceWindu

@MrDave1999 you probably need to enable logging on linq2db side, check this. Otherwise provide your logging configuration code so we can examine what is missing.

Regular EF Core queries log it no problem. I'm trying a Merge query, and it doesn't log. Even with the trace switch turned on.

AntonC9018 avatar Oct 27 '23 07:10 AntonC9018

Specifying a log action works, but we all know that's not a solution.

AntonC9018 avatar Oct 27 '23 07:10 AntonC9018

Yeah, something is not right with logging configuration and we should review it as too many people hit issue being unable to setup it properly. Could you add information what was missing in your configuration?

MaceWindu avatar Oct 27 '23 07:10 MaceWindu

It's basically this:

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContext, services) =>
    {
        var connectionString = hostContext.Configuration
            .GetValue<string>("Configuration:MainDatabaseConnectionString");
        services.AddSqlServer<MyDbContext>(connectionString);
    })
    .ConfigureAppConfiguration(builder =>
    {
        builder.AddJsonFile("appsettings.json");
    })
    .Build();

LinqToDBForEFTools.Implementation = LinqToDbEfCoreImplWithProjectableSupport.Instance;
LinqToDBForEFTools.Initialize();
DataConnection.TurnTraceSwitchOn();
DataConnection.WriteTraceLine = static (message, displayName, _) =>
{
    Console.WriteLine(message);
};

EF Core logs by default, so I expect the lib to do too.

AntonC9018 avatar Oct 27 '23 07:10 AntonC9018

linq2db.EntityFrameworkCore has supported ILoggerFactory from the beginning, starting from EF Core 1. I have implemented logging using this mechanism. However, there is still room for improvement.

Here's an example of how to configure your DbContext with logging enabled:

services.AddDbContext<SomeContext>(options => 
    options
        .UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
        .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug()))
        .EnableSensitiveDataLogging()
    );

You don't need to turn on the trace switch.

sdanyliv avatar Oct 27 '23 08:10 sdanyliv

Thanks

AntonC9018 avatar Oct 27 '23 08:10 AntonC9018