I have a test project, in which I setup an http client like this:
public class TestContext : IDisposable
{
private TestServer _server;
public HttpClient Client { get; private set; }
public TestContext() {
SetUpClient();
}
private void SetUpClient()
{
_server = new TestServer(new WebHostBuilder()
.ConfigureAppConfiguration((builderContext, config) => {
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.UseStartup<Startup>()
.ConfigureServices( services => {
// ...
})
);
Client = _server.CreateClient();
}
}
Using the Startup class of the project to be tested.
In my base TestController I was instantiating a console logger like this:
public WSTestController() {
_log = new LoggerFactory().AddConsole().CreateLogger(this.GetType().Name);
var testContext = new TestContext();
_client = testContext.Client;
// _services = testContext.Services;
}
Now I'm getting the following warning:
controllers\WSTestController.cs(22,20): warning CS0618:
'ConsoleLoggerExtensions.AddConsole(ILoggerFactory)' is obsolete:
'This method is obsolete and will be removed in a future version.
The recommended alternative is AddConsole(this ILoggingBuilder builder).'
I can't get the logger using DI, any idea how I could work with a builder instead of the soon-to-be-deprecated LoggerFactory???
--
I tried with @max-brodin answer but I get the following error:
The following constructor parameters did not have matching fixture data: ILogger log
I guess that it's because the class WSTestController is not a controller of the server created with the new WebHostBuilder() call, so it's not managed by de dependency injection container.
I also tried exposing the app.serviceProvider and then using this.ServiceProvider.GetService(typeof(ILogger));
but it returns null.
I just can't get a console logger from a class that tests a TestServer