65.9K
CodeProject is changing. Read more.
Home

Custom Serilog Sink Development

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jan 16, 2017

CPOL

1 min read

viewsIcon

13331

Custom Serilog Sink Development

sink

The code shown here is part of a VS 2015 solution hosted on GitHub.

If you are coming to this blog post, you probably already know what Serilog is and you need to write to a “sink” that is not already provided in the list of Available Sinks. The list is quite long so definitely look closely before developing your own because there is most likely one already built for your needs.

But what if there isn’t? At the time of this writing, the Developing a Sink link in the Digging Deeper section yields:

comingsoon

Not much help there. But obviously, someone knows how to build one. Keep reading and soon you will be someone too.

Disclaimer: I do not propose that these instructions are necessarily best practice. I cracked open the source code of one of the sinks to see how they did it and I banged on my keyboard until it worked.

The process is actually pretty simple, start by deriving a class from ILogEventSink. Next, implement the Emit method. This is where you do all the real work. In this example, I simply write to the Console but you can go nuts and do whatever you need.

The FormatProvider is not strictly necessary, but I include it for completeness.

using Serilog.Core;
using Serilog.Events;
using System;
 
namespace SeriBlogger
{
    public class StealthConsoleSink : ILogEventSink
    {
        IFormatProvider _formatProvider;
 
        public StealthConsoleSink(IFormatProvider formatProvider)
        {
            _formatProvider = formatProvider;
        }
 
        public void Emit(LogEvent logEvent)
        {
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine(logEvent.RenderMessage(_formatProvider));
            Console.ResetColor();
        }
    }
}

Next, create an extension method on LoggerConfiguration that simply returns an instance of your custom sink. It is the extension method that makes your sink available to the Logger.

using Serilog;
using Serilog.Configuration;
using System;
 
namespace SeriBlogger
{
    public static class StealthConsoleSinkExtensions
    {
        public static LoggerConfiguration StealthConsoleSink(
                  this LoggerSinkConfiguration loggerConfiguration,
                  IFormatProvider fmtProvider = null)
        {
            return loggerConfiguration.Sink(new StealthConsoleSink(fmtProvider));
        }
    }
}

Just like that, you are ready to log to your sink. This one is named StealthConsole because it writes black text on the black background of the console. Super surprised this is not already in the list of available sinks.

static void Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .WriteTo.StealthConsoleSink()
        .CreateLogger();
 
    var loggedby = new { Name = Environment.UserName, Time = DateTimeOffset.Now };
 
    Log.Information("Testing Stealth Console Sink {@loggedby}", loggedby);
 
    Console.ReadKey();
}

Assuming you change your console background to white, this is the result:

console