如何在ASP.NET Core 6中配置和使用Serilog?
由于最近引入了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要确保安装了以下软件包:
.UseSerilog
扩展方法。如果您有 Serilog.AspNetCore 软件包,则不需要明确包含此)然后您需要一个
using
:它应该允许您通过
builder.Host
访问.UseSerilog
:您可以使用不同的过载获取托管上下文、服务和配置。从那里您可以配置接收器等:
You'll need to make sure you have the following packages installed:
.UseSerilog
extension method. If you have the Serilog.AspNetCore package, you do not need to explicitly include this)Then you'll need a
using
:Which should allow you to access
.UseSerilog
viabuilder.Host
:You can use a different overload to get the hosting context, services, and configuration. From there you can configure sinks, etc.:
我使用以下代码片段来解决该问题。我将其添加到 Program.cs
链接
I used the following code snippet to fix the issue. I added it to Program.cs
link
在 Program.cs 类的顶部粘贴以下内容:-
On top of Program.cs class paste the following:-
这是我的工作本地日志记录配置。
首先配置记录器:
然后将 Serilog 添加到构建器日志记录:
要记录日志:
Here is my working local logging configuration.
First configure logger:
Then adding Serilog to builder logging:
To Logging: