简体   繁体   English

Serilog:选择在运行时记录哪个接收器

[英]Serilog : choose which sink to log at runtime

I'm going to implement Serilog within .net standard 2.0 library.我将在 .net 标准 2.0 库中实现 Serilog。 I'm looking for a way to choose which sink(s) should be used for each log line.我正在寻找一种方法来选择每个日志行应该使用哪个接收器。

Lets say that we define 2 sinks within the configuration (console & file):假设我们在配置(控制台和文件)中定义了 2 个接收器:

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .WriteTo.Console()
        .WriteTo.File("c:\\temp\\SerilogTets.txt")
        .CreateLogger();

After this, we are going to write one log rule:在此之后,我们将编写一个日志规则:

Log.Information("Hello, world!");  //<- how can we define which sink we want to use

I'm looking for a way howto define for which sink(s) those lines should be logged for:我正在寻找一种方法来定义应该为哪些接收器记录这些行:

  • Console & File控制台和文件
  • Console only仅限控制台
  • File only仅文件

Without relying on what loglevel it is.不依赖于它是什么日志级别。

Thank you in advance!先感谢您!

Kind regards, Kurt亲切的问候,库尔特

In Serilog you can do this separation via sub-loggers using filters or via Serilog.Sinks.Map , using context properties to decide which logger will include or exclude certain messages.在 Serilog 中,您可以通过使用过滤器的子记录器或通过Serilog.Sinks.Map进行此分离,使用上下文属性来决定哪个记录器将包含或排除某些消息。

The example below defines that all log events will be written to both the Console and to a File, by default, however if the log event has a property called FileOnly in the log context, it will not be written to the Console, and likewise if the log event has a property called ConsoleOnly , it will not be written to the File.下面的示例定义了所有日志事件都将写入控制台和文件,默认情况下,但是如果日志事件在日志上下文中有一个名为FileOnly的属性,则不会将其写入控制台,同样如果日志事件有一个名为ConsoleOnly的属性,它不会被写入文件。

using Serilog;
using Serilog.Context;
using Serilog.Filters;
// ...

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .WriteTo.Logger(c =>
        c.Filter.ByExcluding(Matching.WithProperty("FileOnly"))
            .WriteTo.Console())
    .WriteTo.Logger(c =>
        c.Filter.ByExcluding(Matching.WithProperty("ConsoleOnly"))
            .WriteTo.File("C:\\Temp\\SerilogTests.txt"))
    .CreateLogger();

// Writes to both Console & File
Log.Information("Hello, world! (Console and File)");

using (LogContext.PushProperty("ConsoleOnly", value: true))
{
    // Writes only to the Console
    Log.Information("Hello, world! (Console)");
}

using (LogContext.PushProperty("FileOnly", value: true))
{
    // Writes only to the File
    Log.Information("Hello, world! (File Only)");
}

Log.CloseAndFlush();

NB: Ideally you'll come up with better property names that are more generic, so that when you are writing logs within your application it doesn't have to know anything about "Console" or "File".注意:理想情况下,您会想出更通用的更好的属性名称,这样当您在应用程序中编写日志时,它不必知道任何有关“控制台”或“文件”的信息。 eg You could have a property called Secret or Classified and decide where to write the logs based on the presence of this property or not.例如,您可以拥有一个名为SecretClassified的属性,并根据该属性的存在与否决定在何处写入日志。

There are different ways to add properties to a log event, including adding the property in the message template directly when you Log.Information , etc., via LogContext.PushProperty as in the example above, and vi Log.ForContext .向日志事件添加属性有多种方法,包括在Log.Information等时直接在消息模板中添加属性,如上例中通过LogContext.PushProperty和 vi Log.ForContext

You can see other examples of filtering, sub-loggers, and Serilog.Sinks.Map here:您可以在此处查看过滤、子记录器和 Serilog.Sinks.Map 的其他示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM