【发布时间】:2019-08-30 20:24:28
【问题描述】:
我目前在 VSTS 中的构建机器上的测试失败了,真的很难知道发生了什么。我有很多由 API 生成的日志,这些日志可能真的很有帮助。
我的目的是将我的 API 中生成的所有日志重定向到 OutputTestHelper。
使用 Xunit,我有一个 TestServerFixture:
public class TestServerFixture : IDisposable
{
public TestServer Server { get; }
public HttpClient Client { get; }
public T GetService<T>() => (T)Server.Host.Services.GetService(typeof(T));
public TestServerFixture()
{
var builder = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseEnvironment("Development")
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
})
.UseStartup<TestStartup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddXunit(output); //How Can I get ITestOuputHelper?
});
Server = new TestServer(builder);
Client = Server.CreateClient();
}
public void Dispose()
{
Server.Dispose();
Client.Dispose();
}
}
阅读:
我有一个扩展程序允许我将 Xunit ITestOutputHelper 添加到需要 ITestOutputHelper 作为参数的记录器 (AddXunit())。
问题是,我找不到在 TestServerFixture 中获取它的方法。我试过了:
也许在 TestServerFixture 的构造函数中有一些可用的 Injection 可以实例化 ITestOutputHelper ?
public TestServerFixture(ITestOutputHelper output)=> 失败也许有办法从 IloggingBuilder.Services 中解决 ITestOutputHelper(如
loggingBuilder.Services.Resolve<ITestOutputHelper>())=> FAILED也许我可以从 TestStartup 中的 Configure 方法获得它(如
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, ITestOutputHelper output))=> FAILED
您知道配置 TestServerFixture 以将所有日志重定向到 OutpuTestHelper 的方法吗?
【问题讨论】: