cod*_*rsl 9 c# entity-framework
如何启用DbCommand原始SQL查询的日志记录?
我已将以下代码添加到我的Startup.cs文件中,但未看到Entity Framework Core中的任何日志条目.
void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(LogLevel.Debug);
}
Run Code Online (Sandbox Code Playgroud)
我期待看到这样的事情:
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilder...
SELECT [t].[Id], [t].[DateCreated], [t].[Name], [t].[UserName]
FROM [Trips] AS [t]
Run Code Online (Sandbox Code Playgroud)
小智 6
从 MVC Core 2 开始,记录 SQL 是默认行为。只需确保 appSettings json 文件中的日志记录级别正确即可。
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
Run Code Online (Sandbox Code Playgroud)
想通了 - 需要配置 DbContext 以使用记录器工厂。
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseLoggerFactory(_loggerFactory);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10232 次 |
| 最近记录: |