Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#6.0

Custom Serilog Sink Development

5.00/5 (1 vote)
16 Jan 2017CPOL1 min read 13.3K  
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.

C#
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.

C#
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.

C#
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

Image 4 Image 5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)