On an AspNetCore 5 Web and API Projects I had the following on Program files:
public class Program {
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", false, true)
.AddJsonFile($"settings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", false, true)
.AddEnvironmentVariables()
.Build();
public static Int32 Main(String[] args) {
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(String[] args) {
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder
.UseSerilog()
.ConfigureWebHostDefaults(builder => {
builder
.UseConfiguration(Configuration)
.UseStartup<Startup>();
});
return builder;
}
}
Now I am moving this code to NET 6 using Program.cs
without Startup.cs
:
WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", false, true)
.AddJsonFile($"settings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", false, true)
.AddEnvironmentVariables();
builder.Host.UseSerilog();
builder.Host.ConfigureWebHostDefaults(x => {
x.UseSerilog(); / ??
x.UseConfiguration(??); // ??
x.UseWebRoot("webroot");
});
await using WebApplication application = builder.Build();
Question
Can I set configuration in
builder.Configuration
or should I do it inside ConfigureWebHostDefaults?Should I use
builder.Host.UserSerilog
or add Serilog inConfigureWebHostDefaults
?
It's a bit confusing because I can do things in different ways.