Serilog:记录到不同的文件 [英] Serilog : Log to different files

查看:87
本文介绍了Serilog:记录到不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将所有类型的事件记录到单个 Json 文件中,而不考虑 LogLevel.现在我需要将一些自定义性能计数器记录到单独的 Json 文件中.这如何在 Serilog 中完成.我应该创建不同的 Logger 实例并在我要记录性能计数器的地方使用它吗?想和 LibLog 一起使用

I am logging events of all types to single Json file irrespective of LogLevel. Now I have a requirement to log some custom performance counters to a seperate Json file. How can this be done in Serilog. Should I create different Logger Instance and use that where ever I am going to Log the performance counters? Want to use this with LibLog

推荐答案

您可以通过首先确保性能计数器事件使用特定属性值(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();

这篇关于Serilog:记录到不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆