我有使用 IdentityServer3
的 Web asp.net MVC 应用程序。
我想将日志记录分割到不同的接收器。
我想将 IdentityServer3
日志写入 Trace
、EventLog("IdentityServer3")
和 SerilogWeb.Classic.Enrichers
到数据库MSSqlServer
。
我的配置是:
Log.Logger = new LoggerConfiguration()
.WriteTo.Trace()
.WriteTo.EventLog("IdentityServer3")
.Enrich.FromLogContext()
//all after this i want to write in database
.WriteTo.MSSqlServer("connectionString", "Log")
.Enrich.With(new HttpRequestIdEnricher())
.Enrich.With(new HttpRequestRawUrlEnricher())
.Enrich.With(new HttpRequestUserAgentEnricher())
.Enrich.With(new UserNameEnricher())
.CreateLogger();
最佳答案
要将事件子集写入接收器,请使用 WriteTo.Logger()
和 Filter
指令。每个案例都会相似;这是身份服务器:
Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(Matching.FromSource("IdentityServer3"))
.WriteTo.Trace()
.WriteTo.EventLog("IdentityServer3"))
// <snip>
您可以在最外层应用丰富器,以便它们丰富所有事件,也可以在嵌套记录器配置之一上应用丰富器。
关于c# - Serilog LoggerConfiguration通过上下文写入不同的Sink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41004325/