Serilog :选择在运行时记录哪个接收器 [英] Serilog : choose which sink to log at runtime

查看:54
本文介绍了Serilog :选择在运行时记录哪个接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在 .net 标准 2.0 库中实现 Serilog.我正在寻找一种方法来选择每个日志行应该使用哪个接收器.

I'm going to implement Serilog within .net standard 2.0 library. I'm looking for a way to choose which sink(s) should be used for each log line.

假设我们在配置(控制台和文件)中定义了 2 个接收器:

Lets say that we define 2 sinks within the configuration (console & file) :

    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 :

  • 控制台和文件
  • 仅限控制台
  • 仅文件

不依赖于它是什么日志级别.

Without relying on what loglevel it is.

先谢谢你!

亲切的问候,库尔特

推荐答案

在 Serilog 中,您可以通过 子记录器 使用 过滤器或通过Serilog.Sinks.Map,使用上下文属性 来决定哪个记录器将包含或排除某些消息.

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.

下面的示例定义了所有日志事件将被写入控制台和文件,默认情况下,但是如果日志事件在日志上下文中有一个名为 FileOnly 的属性,它将不会写入控制台,同样,如果日志事件具有名为 ConsoleOnly 的属性,则不会写入文件.

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.

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();

注意:理想情况下,您会想出更通用的更好的属性名称,这样当您在应用程序中编写日志时,它就不必了解有关控制台"的任何信息.或文件".例如您可以拥有一个名为 SecretClassified 的属性,并根据此属性的存在与否决定将日志写入何处.

N.B.: 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". e.g. 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.

给日志事件添加属性有多种方式,包括在Log.Information时直接在消息模板中添加属性等,通过LogContext.PushProperty 如上例所示,以及 vi Log.ForContext.

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.

您可以在此处查看过滤、子记录器和 Serilog.Sinks.Map 的其他示例:

You can see other examples of filtering, sub-loggers, and Serilog.Sinks.Map here:

这篇关于Serilog :选择在运行时记录哪个接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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