如何在ASP.NET Core 6中配置和使用Serilog?

发布于 2025-01-16 11:55:33 字数 778 浏览 1 评论 0原文

由于最近引入了 Program.cs 启动代码的新结构,该文档让我有点困惑。

在官方提供的Serilog.AspNetCore示例Serilog.Sentry 示例,他们在 WebHostBuilder 上使用 .UseSerilog()。我找不到这个方法。

这是我尝试过的:

using Serilog;

var builder = WebApplication.CreateBuilder(args);
// adding services...

builder.Logging.AddSerilog(); // <- is this even necessary?

var app = builder.Build();
app.UseSerilogRequestLogging();

// configure request pipeline
app.Run();

但是如何/在哪里配置接收器,例如调试、控制台、哨兵……?我觉得文档有点过时,或者我只是有点盲目。

Since the recently introduced new structure of the Program.cs startup code, the documentation confuses me a bit.

In the officially provided Serilog.AspNetCore example and in the Serilog.Sentry example, they use .UseSerilog() on the WebHostBuilder. I cannot find this method.

This is what I have tried:

using Serilog;

var builder = WebApplication.CreateBuilder(args);
// adding services...

builder.Logging.AddSerilog(); // <- is this even necessary?

var app = builder.Build();
app.UseSerilogRequestLogging();

// configure request pipeline
app.Run();

But how / where can I configure the sinks, e.g. Debug, Console, Sentry, ...? I have the feeling that docs are a bit outdated or I am just a bit blind.

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

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

发布评论

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

评论(5

时光倒影 2025-01-23 11:55:33

您需要确保安装了以下软件包:

  • Serilog
  • Serilog.Extensions.Hosting (这提供了 .UseSerilog 扩展方法。如果您有 Serilog.AspNetCore 软件包,则不需要明确包含此)

然后您需要一个 using

using Serilog;

它应该允许您通过 builder.Host 访问 .UseSerilog

using Serilog;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

您可以使用不同的过载获取托管上下文、服务和配置。从那里您可以配置接收器等:

builder.Host.UseSerilog((hostContext, services, configuration) => {
    configuration.WriteTo.Console();
});

You'll need to make sure you have the following packages installed:

  • Serilog
  • Serilog.Extensions.Hosting (this provides the .UseSerilog extension method. If you have the Serilog.AspNetCore package, you do not need to explicitly include this)

Then you'll need a using:

using Serilog;

Which should allow you to access .UseSerilog via builder.Host:

using Serilog;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

You can use a different overload to get the hosting context, services, and configuration. From there you can configure sinks, etc.:

builder.Host.UseSerilog((hostContext, services, configuration) => {
    configuration.WriteTo.Console();
});
世界等同你 2025-01-23 11:55:33

我使用以下代码片段来解决该问题。我将其添加到 Program.cs

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.CreateLogger();

builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);

链接

I used the following code snippet to fix the issue. I added it to Program.cs

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.CreateLogger();

builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);

link

帥小哥 2025-01-23 11:55:33

在 Program.cs 类的顶部粘贴以下内容:-

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/rumble-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();

On top of Program.cs class paste the following:-

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/rumble-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
深空失忆 2025-01-23 11:55:33

这是我的工作本地日志记录配置。
首先配置记录器:

Log.Logger = new LoggerConfiguration().WriteTo.File(
    path: "c:\\hotellisting\\log\\log-.txt",
    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
    rollingInterval: RollingInterval.Day,
    restrictedToMinimumLevel: LogEventLevel.Warning
    ).CreateLogger();

然后将 Serilog 添加到构建器日志记录:

builder.Logging.AddSerilog();

要记录日志:

Log.Error("error occured while bla bla");

Here is my working local logging configuration.
First configure logger:

Log.Logger = new LoggerConfiguration().WriteTo.File(
    path: "c:\\hotellisting\\log\\log-.txt",
    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
    rollingInterval: RollingInterval.Day,
    restrictedToMinimumLevel: LogEventLevel.Warning
    ).CreateLogger();

Then adding Serilog to builder logging:

builder.Logging.AddSerilog();

To Logging:

Log.Error("error occured while bla bla");
忆沫 2025-01-23 11:55:33
//serilog
Log.Logger = new LoggerConfiguration()
    
    .WriteTo.File(new JsonFormatter(),@"logs\log-.txt", rollingInterval: RollingInterval.Hour)
    .MinimumLevel.Error()
    .WriteTo.Console(new JsonFormatter())
    .CreateLogger();
builder.Host.UseSerilog(Log.Logger);
var app = builder.Build();
//serilog
Log.Logger = new LoggerConfiguration()
    
    .WriteTo.File(new JsonFormatter(),@"logs\log-.txt", rollingInterval: RollingInterval.Hour)
    .MinimumLevel.Error()
    .WriteTo.Console(new JsonFormatter())
    .CreateLogger();
builder.Host.UseSerilog(Log.Logger);
var app = builder.Build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文