Click here to Skip to main content
15,113,057 members
Articles / Programming Languages / C# 6.0
Technical Blog
Posted 16 Jan 2017


2 bookmarked

Custom Serilog Sink Development

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
16 Jan 2017CPOL1 min read
Custom Serilog Sink Development


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:


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;

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()
    var loggedby = new { Name = Environment.UserName, Time = DateTimeOffset.Now };
    Log.Information("Testing Stealth Console Sink {@loggedby}", loggedby);

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


Image 4 Image 5


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


About the Author

Bob Crowley
Software Developer (Senior) EVO Payments International
United States United States
Full stack developer on .Net and related technologies with heavy emphasis on back-end API development and integrations.

Comments and Discussions

QuestionThat's a good summary, simple and efficient Pin
Jean-Philippe Gravel18-Aug-17 6:53
professionalJean-Philippe Gravel18-Aug-17 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.