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.
CodePudding user response:
1. Can I set configuration in builder.Configuration
or should I do it inside ConfigureWebHostDefaults
?
To manage configuration you should use ConfigurationManager
exposed by builder.Configuration
:
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", false, true)
.AddJsonFile($"settings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", false, true)
.AddEnvironmentVariables();
2. Should I use builder.Host.UserSerilog
or add Serilog
in ConfigureWebHostDefaults
From Nicholas Blumhardt's (one of the Serilog maintainers) github sample use builder.Host
:
builder.Host.UseSerilog((ctx, lc) => // Serilog setup here, use ctx.Configuration for config);
CodePudding user response:
The new hosting model uses WebApplicationBuilder
to configure the web-host defaults, so there's no need for you to call this yourself.
Can I set configuration in
builder.Configuration
or should I do it insideConfigureWebHostDefaults
?
Do this using builder.Configuration
.
Should I use
builder.Host.UserSerilog
or add Serilog inConfigureWebHostDefaults
?
Do this using builder.Host
. Nicholas Blumhardt demonstrates this in Setting up Serilog in .NET 6.
It's a bit confusing because I can do things in different ways.
There's a strong need for backwards compatibility, especially as .NET 6 apps aren't required to use the new hosting model. This results in a few different approaches being available to you. I expect this is something that will change with future releases, but that's merely speculation on my part.