linq2db.EntityFrameworkCore
linq2db.EntityFrameworkCore copied to clipboard
Improve/document query logging setup
How can I visualize the generated SQL code? It is not displayed in the Visual Studio output box.
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/
Closing as answered
@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 you probably need to enable logging on linq2db side, check this. Otherwise provide your logging configuration code so we can examine what is missing.
@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.
Specifying a log action works, but we all know that's not a solution.
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?
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.
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.
Thanks