使用Serilog自定义LogEventLevel

Rég*_*IOX 3 .net logging seq serilog

我在.Net项目中使用Serilog和Seq。我的主要目标是记录一些特定事件。我想拥有自己的日志事件级别(例如“警告”,“详细”,“信息” ...)。但是我真的不知道是否有可能拥有我的自定义日志级别:

private static readonly ILogger Logger = Log.ForContext<MyController>();
        .
        .
        .
Logger.MyCustomLogLevel(....);
Run Code Online (Sandbox Code Playgroud)

可能吗?先感谢您

Nic*_*rdt 6

尽管无法实现自定义级别,但想象一下要(逻辑上)创建和Important级别,您可以使用以下方法实现接近相同的目的:

static class LoggerExtensions
{
  public static void Important(
    this ILogger logger,
    string messageTemplate,
    params object[] args)
  {
    logger.ForContext("IsImportant", true)
      .Information(messageTemplate, args);
  }
}
Run Code Online (Sandbox Code Playgroud)

用法:

Logger.Important("Hello, {Name}!", "World");
Run Code Online (Sandbox Code Playgroud)

在序列中:

IsImportant = true
Run Code Online (Sandbox Code Playgroud)

要么:

select count(*) from stream where IsImportant limit 10
Run Code Online (Sandbox Code Playgroud)