EF Core:看不到任何查询日志?

问题描述 投票:0回答:2

我正在尝试查看 EF 正在执行什么...它在新项目上运行良好,但我似乎无法让 serilog 打印它? 我像这样设置覆盖;

var host = Host.CreateDefaultBuilder()
    .ConfigureServices((context, collection) => ServerServiceCollection.AddServices(collection, context.Configuration))
    .UseSerilog((hostContext, _, logger) => 
        logger.ReadFrom.Configuration(hostContext.Configuration)
        .MinimumLevel.Override("Microsoft", LogEventLevel.Verbose))
    .Build();
c# entity-framework serilog
2个回答
0
投票

我不熟悉 Serilog 选项,但替代方案可以覆盖 DbContext OnConfiguring 方法。

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.LogTo(Console.WriteLine);

这会将这个特定 DbContext 上的所有查询打印到控制台。然后,您可以将 .Tagwith($"{tagName}") 附加到查询中以改进控制台中的过滤,或者您可以更改 OnConfiguring 并在其中添加过滤器。

更多信息可以在这里查看:https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/

希望这能有所帮助:)


0
投票

您需要在选项构建器中添加日志记录,无论是在传递给构造函数之前还是在

OnConfiguring
方法中。

optionsBuilder
    .LogTo(Serilog.Log.Logger.Debug, LogLevel.Information, DbContextLoggerOptions.SingleLine)
    .EnableSensitiveDataLogging(System.Diagnostics.Debugger.IsAttached);

EnableSensitiveDataLogging
是可选的,但在调试时通常很有用。

© www.soinside.com 2019 - 2024. All rights reserved.