且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Serilog:记录到不同的文件

更新时间:2023-02-17 08:16:29

您可以通过首先确保性能计数器事件使用特定属性值(LibLog 中的OpenMappedContext()) 或来自特定类型/命名空间.

You can do this by first making sure the performance counter events are tagged with either a particular property value (OpenMappedContext() in LibLog) or from a particular type/namespace.

var log = LogProvider.For<MyApp.Performance.SomeCounter>()
log.Info(...);

在配置 Serilog 时,子记录器应用的过滤器可以只将所需的事件发送到第二个文件.

When configuring Serilog, a sub-logger with filter applied can send just the required events to the second file.

Log.Logger = new LoggerConfiguration()
    .WriteTo.Logger(lc => lc
        .Filter.ByExcluding(Matching.FromSource("MyApp.Performance"))
        .WriteTo.File("first.json", new JsonFormatter()))
    .WriteTo.Logger(lc => lc
        .Filter.ByIncludingOnly(Matching.FromSource("MyApp.Performance"))
        .WriteTo.File("second.json", new JsonFormatter()))
    .CreateLogger();