How to handle deprecation of IWebHostBuilder.UseSerilog() when IWebHostBuilder is still needed?
Asked Answered
S

3

31

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

Shock answered 21/2, 2022 at 16:7 Comment(2)
https://mcmap.net/q/471691/-how-to-add-configuration-in-net-6-program-cs-only – Cerebrospinal
Thanks @RubenBartelink. That post doesn't seem to address my question as I'm using IWebHostBuilder. – Shock
S
28

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();
}
Shock answered 3/3, 2022 at 16:32 Comment(1)
This works... now how do I do it for TestServer πŸ˜… – Orville
O
2

If you use TestServer (e.g. for integration tests), similar to the accepted answer, you need to move from WebHostBuilder to HostBuilder.

Note you can't use new TestServer(...) anymore, as there's no constructor which takes IHostBuilder. Need the UseTestServer() method instead.


Example:

Before:

var testServer = new TestServer(
    new WebHostBuilder()
        .UseSerilog()
        .UseConfiguration(Configuration)
        .UseStartup<Startup>()
);

var client = testServer.CreateClient();

After:

var host = new HostBuilder()
    .UseSerilog()
    .ConfigureWebHost(
        webBuilder => webBuilder
            .UseConfiguration(Configuration)
            .UseStartup<Startup>()
            .UseTestServer()
    )
    .Build();

await host.StartAsync();

var client = host.GetTestClient();
Orville answered 1/5 at 8:40 Comment(0)
S
0

I Solved with:

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
Shutz answered 6/4, 2022 at 14:35 Comment(1)
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. – Shock

© 2022 - 2024 β€” McMap. All rights reserved.