本文介绍了ASP.NET - Serilog - 多个接收器,每个接收器用于不同用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Serilog 进行日志记录,因为我使用它并且它符合我的需求.

I want to use Serilog for logging because I worked with it and it fits my needs.

在这种情况下,不同之处在于我需要记录不同的模块,并且我希望每个模块都记录在不同的文件夹中,甚至可能在不同的接收器中.

The difference in this case is that I need to log different modules and I would like each module to be logged in a different folder and even maybe in a different sink.

我不是在谈论使用多个接收器,而是在谈论做这样的事情:

I am not talking about using several sinks, but I talk about doing something like this:

Serilog.Log(LogInstance1,"Log for module 1") // Logs in a rolling text file in folder X 10 files of 1MB each
Serilog.Log(LogInstance2,"Log for module 2") // Logs i na text file that doesn't delete old entries

有谁知道这是否可以通过 Serilog 实现?

Does anyone know if this is possible to achieve with Serilog?

推荐答案

您可以创建多个单独的 ILogger 实例,每个实例都有自己的配置和接收器集等;每个管道完全独立运行,没有任何类型的共享状态.

You can make multiple separate ILogger instances, each with their own configuration and set of sinks etc.; each pipeline operates completely separately with no shared state of any kind.

var logger1 = new LoggerConfiguration().
    .WriteTo.File(...10...)
    .CreateLogger();

var logger2 = new LoggerConfiguration()
    .WriteTo.File(...)
    .CreateLogger();

logger1.Information("Log for Module 1");
logger2.Information("Log for Module 2");

这篇关于ASP.NET - Serilog - 多个接收器,每个接收器用于不同用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 06:25