kestrel项目中UseSerilog的使用方法

How to use UseSerilog in kestrel project

我有这个引导代码,我正在尝试找出如何向其中添加 Serilog。

using Microsoft.AspNetCore.Components.WebAssembly.Server ;
using System.Net;
using Serilog;
using Serilog.AspNetCore;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment()) {
}
else {
    builder.Services.AddLettuceEncrypt();
    builder.WebHost.UseKestrel(k => {
        var appServices = k.ApplicationServices;
        k.Listen(IPAddress.Any, 80);
        k.Listen(IPAddress.Any, 443,
            o => o.UseHttps(h => {
              h.UseLettuceEncrypt(appServices);
            }));

        k.ConfigureHttpsDefaults(h => {
            h.UseLettuceEncrypt(appServices);
        });
    });
}
var app = builder.Build();
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.MapControllers();
app.MapFallbackToFile("index.html");

app.Run();

我找到了 Serilog.AspNetCore 并安装了那个包。 github 主页 https://github.com/serilog/serilog-aspnetcore 说要添加 UseSerilog,但我不知道如何连接它。

我加了

// Add services to the container.

builder.Services.AddControllers();
builder.WebHost.UseSerilog(); <<<====

但这已被弃用并表示

warning CS0618: 'SerilogWebHostBuilderExtensions.UseSerilog(IWebHostBuilder, ILogger, bool, LoggerProviderCollection)' is obsolete: 'Prefer UseSerilog() on IHostBuilder'

但我没有 IHostBuilder,也不知道如何制作。我也找不到带有该扩展名的包裹

IHostBuilderbuilder.Host 上。
来自 documentation

An IHostBuilder for configuring host specific properties, but not building.

调用配置 Serilog 变为

builder.Host.UseSerilog();