Search code examples
.netloggingseqserilog

Custom LogEventLevel with Serilog


I'm using Serilog and Seq with a .Net project. My main objective is to log some specific events. I would like to have my own log event level (like "Warning", "Verbose", "Information"...). But I don't really know if it's possible or not to have my custom log level for having this:

private static readonly ILogger Logger = Log.ForContext<MyController>();
        .
        .
        .
Logger.MyCustomLogLevel(....);

Is it possible?


Solution

  • Although custom levels are not possible, imagining you want to create (logically) and Important level you can achieve close to the same thing with:

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

    Usage:

    Logger.Important("Hello, {Name}!", "World");
    

    In Seq:

    IsImportant = true
    

    Or:

    select count(*) from stream where IsImportant limit 10