Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C# 6.0

Custom Serilog Sink Development

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
16 Jan 2017CPOL1 min read 12.3K   3   1
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)


Written By
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 5:53
professionalJean-Philippe Gravel18-Aug-17 5: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.