1

I have this project made from core 3.1 and upgrading to 6 but I'm having problems with the obsolete UseSerilog() method. With the new minimal WebApplication.CreateBuilder, UseSerilog() now requires builder.Host and my problem is that UserKestrel() uses builder.WebHost.

Are there any workaround to do this? I tried below code: using builder.WebHost and builder.Host in the same builder to setup both but I don't know if there are any downsides.

var builder = WebApplication.CreateBuilder(args);

builder.Services.RegisterServices(builder.Configuration);

builder.WebHost
    .UseKestrel(options => options.AddServerHeader = false)
    .UseContentRoot(Directory.GetCurrentDirectory())
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        var env = hostingContext.HostingEnvironment;
        Console.WriteLine($@"Application: {env.ApplicationName}");
        Console.WriteLine($@"Environment: {env.EnvironmentName}");

        config.SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", false, true, new[] { "ConnectionStrings:ConString" })
            .AddJsonFile($"appsettings.{env.EnvironmentName.ToLowerInvariant()}.json", true, true)
            .AddEnvironmentVariables("ASPNETCORE_");
    })
    .UseIISIntegration()
    .CaptureStartupErrors(false)
    .UseHealthChecks("/healthcheck");

builder.Host.UseSerilog((ctx, lc) => lc.ReadFrom.Configuration(ctx.Configuration));

var app = builder.Build();

app.Run();
user3389678
  • 11
  • 1
  • 4
  • And [this](https://stackoverflow.com/questions/69904260/configuring-kestrel-server-options-in-net-6-startup/69904470#69904470) – Guru Stron Oct 04 '22 at 07:34
  • 1
    Also note that "new" minimal hosting model is not required during the upgrade, you still can use the generic one (with `Startup`). – Guru Stron Oct 04 '22 at 07:35
  • @Dai We're having no problems so far using UseIISIntegration and UseKestrel but will look it up – user3389678 Oct 04 '22 at 08:21
  • @user3389678 I deleted my comment because I'm probably wrong. – Dai Oct 04 '22 at 08:22

0 Answers0