I was able to get this to work by switching to IHostBuilder
instead of IWebHostBuilder
. The key in my case was to call ConfigureWebHostDefaults
(or ConfigureWebHost
), where you can then utilize an IWebHostBuilder
.
In this way, I could call UseSerilog
on the IHostBuilder
while still utilizing the Startup
class as before with an IWebHostBuilder
.
Example:
public static void Main(string[] args)
{
// temp initial logging
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
using var app =
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder
=> webHostBuilder.ConfigureAppConfiguration((hostingContext, config) =>
_ = config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", false, true))
.UseStartup<Startup>())
.UseSerilog(
(hostingContext, loggerConfig) =>
loggerConfig
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext(),
writeToProviders: true)
.Build();
app.Run();
}