18

The UseSerilog() extension is deprecated* for IWebHostBuilder in serilog-aspnetcore version 5.0.0. However I'm still using a IWebHostBuilder and a Startup class (aspnetcore 6), and IWebHostBuilder is not deprecated.

Since deprecation implies future removal, how should I leverage Serilog going forward?


reference: https://github.com/serilog/serilog-aspnetcore/releases/tag/v5.0.0 "mark IWebHostBuilder extensions as obsolete on platforms with IHostBuilder"

daniel327
  • 331
  • 2
  • 10

2 Answers2

15

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();
}
daniel327
  • 331
  • 2
  • 10
1

I Solved with:

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
Gabriel_ES
  • 13
  • 2
  • 7
    I believe that assumes you're using the new minimal hosting model. As stated, I'm using IWebHostBuilder and a Startup class, so this isn't applicable. – daniel327 Apr 06 '22 at 18:18