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