Serilog, how do I use Sinks with the LoggingBuilder approach?

Hi,
I would like to use the Serilog email sink.
Serilog works with IloggerBuilder but many of is adapters use an older interface called Sinks. I can get extensions working that you cover in your tutorial (timestamped) but I can’t understand how to plug Sinks in. According to the nuget information this is how, but I can’t see where LoggerConfiguration() comes from in the interface or if it should be cast.
It says

in your application’s Startup method, configure Serilog first:

using Serilog;

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        Log.Logger = new LoggerConfiguration()
          .Enrich.FromLogContext()
          .WriteTo.Console()
          .CreateLogger();

        // Other startup code

Finally, for .NET Core 2.0+, in your Startup class’s Configure() method, remove the existing logger configuration entries and call AddSerilog() on the provided loggingBuilder.

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(loggingBuilder =>
        loggingBuilder.AddSerilog(dispose: true));

    // Other services ...
}

Hey @Hadasi,

This is how I got it working with the Loki sink (removed all input pins for clarity) :

I would then use this AddLoki operation directly in my IStartup class.

Hope this helps!

2 Likes

@sebescudie thanks for the reply, yeah spot on!
In the past I always used Sink functions with a provider (probably because the Logging builder was unknown to me at the time), so I forgot or didn’t realize that sinks came with the extension functions too.
Thanks a bunch!

Quick additional note as of writing, vvvv uses .NET 8 and Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.0 but the latest updated Serilog Sinks use Microsoft.Extensions.Logging.Abstractions, Version=9.0.0.0
Use nuget install Serilog.Extensions.Logging -Version 8.0.0 for it to work without complaints

2 Likes