如何在Kestrel项目中使用useserilog

发布于 2025-01-21 22:51:10 字数 1708 浏览 3 评论 0原文

我有此引导代码,并且正在尝试核算如何将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 home https://github.com/serilog/serilog-aserilog-aspnetcore 但是我无法弄清楚如何将其连接

// 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',但我没有一个iHostbuilder and ihostbuilder and One of sone。我也不能找到带有该扩展程序的包裹

I have this boot code and I am trying to work out how to add Serilog to it.

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();

I found Serilog.AspNetCore and have installed that package. The github home https://github.com/serilog/serilog-aspnetcore says to add UseSerilog but I cannot work out how to wire that in.

I added

// Add services to the container.

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

But this is deprecated and says

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

But I dont have an IHostBuilder and dont see how to make one. Nor can I find a package with that extension

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-01-28 22:51:10

iHostBuilderbuilder.host
来自

iHostBuilder用于配置主机特定属性,但不建立。

配置serilog的调用变为

builder.Host.UseSerilog();

That IHostBuilder is on builder.Host.
From the documentation

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

The call to configure Serilog becomes

builder.Host.UseSerilog();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文