小编kwl*_*kwl的帖子

让 EF Core 将 SQL 语句输出到 xUnit 的 ITestOutputHelper

我正在使用 EF Core 2.2.4 并试图找出 EF Core 在我们的单元测试中将哪些 SQL 语句发送到我们的 SQLite 数据库。由于我们使用的是 xUnit (2.4.1),我们必须将日志消息写入ITestOutputHelperxUnit 注入我们的测试类而不是控制台的实例中。对于控制台,我找到了这个代码:

private static ILoggerFactory GetLoggerFactory()
{
    IServiceCollection serviceCollection = new ServiceCollection();
    serviceCollection.AddLogging(builder =>
        builder.AddConsole()
            .AddFilter(DbLoggerCategory.Database.Command.Name,
                LogLevel.Information));
    return serviceCollection.BuildServiceProvider()
        .GetService<ILoggerFactory>();
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能将此输出重定向到ITestOutputHelper.WriteLine()

xunit xunit.net entity-framework-core ef-core-2.2

2
推荐指数
1
解决办法
556
查看次数