-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aspnetcore 7 WebApplicationFactory doesn't get default minimum configuration correctly #332
Comments
Hi! Thanks for the report - any chance of a code sample/failing test that demonstrates the issue? |
Hmm, I made the incorrect diagnosis . I hit this using https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0 with net7.0. What happens is that the WebApplicationFactory forwards a bunch of configuration as arguments for the program. for an appsetting of {
"Serilog": {
"MinimumLevel": {
"Default": "Information",
}
}
} It sets the below as arguments var configuration = new ConfigurationBuilder()
.AddCommandLine(new []
{
"--Serilog=",
"--Serilog:MinimumLevel=",
"--Serilog:MinimumLevel:Default=Information"
})
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger(); which the logger configuration cannot handle. Something changed in how they set the configuration resulted in this new behavior. |
repro https://github.com/wzchua/WebApplicationFactoryBugWithSerilogConfiguration public class UnitTest1 :
IClassFixture<CustomWebApplicationFactory<Program>>
{
private readonly HttpClient _client;
private readonly CustomWebApplicationFactory<Program>
_factory;
public UnitTest1(CustomWebApplicationFactory<Program> factory)
{
_factory = factory;
_client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
[Fact]
public async Task Test1()
{
var message = await _client.GetAsync("/");
}
}
public class CustomWebApplicationFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureHostConfiguration(configurationBuilder =>
{
configurationBuilder.AddInMemoryCollection(new Dictionary<string, string?>
{
{"Serilog:MinimumLevel:Default", "Debug"},
});
});
builder.UseSerilog((context, configuration) =>
{
configuration.ReadFrom.Configuration(context.Configuration);
configuration.WriteTo.Debug();
});
return base.CreateHost(builder);
}
} |
Okay I can workaround this by using I am not sure if this is an issue for this library. You can close this if it's not actionable. |
Thanks for all the details, sounds like it needs some investigation 👍 |
I think using ConfigureAppConfiguration is the right thing to do:
Using ConfigureHostConfiguration is not appropriate to configure Serilog:
|
I'm pretty sure there is a bug here though I am not entirely sure where (e.g. Basically when using // appsettings.json
…
"Serilog": {
"MinimumLevel": {
"Default": "Information"
}
}
… This throws an exception like the following:
Note the double space in there: But the following works fine: // appsettings.json
…
"Serilog": {
"MinimumLevel": "Information"
}
… I am using code like the following to set this up but I don't think it matters: public class ComponentTestsFixture : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
builder.UseConfiguration(config.Build());
}
} I've also tried using |
@peabnuts123 a fix would be welcome if you can track this down, I don't have anything to add. |
The min repro is
the fix likely would involve ignoring the key |
Just run into this myself, be great if we could get a release with the fix in it! Cheers |
Checking for the minimum section returns
""
instead of null, so the logic doesn't get the value fromDefault
The text was updated successfully, but these errors were encountered: