serilog / serilog-sinks-periodicbatching

Infrastructure for Serilog sinks that process events in batches.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serilog.Sinks.PeriodicBatching Build status NuGet Version

A wrapper for Serilog sinks that asynchronously emits events in batches, useful when logging to a slow and/or remote target.

Important

Serilog 4.x and later versions support batching natively. New projects should use Serilog's IBatchedLogEventSink and WriteTo.Sink(IBatchedLogEventSink), not this package which is now only maintained for compatibility reasons.

Updating for Serilog v4+

First, update your Serilog package reference to the latest published version.

This example is from Serilog.Sinks.Postgresql.Alternative. Old code:

var batchingOptions = new PeriodicBatchingSinkOptions()
{
    BatchSizeLimit = postgresOptions.BatchSizeLimit,
    Period = postgresOptions.Period,
    QueueLimit = postgresOptions.QueueLimit
};

var batchingSink = new PeriodicBatchingSink(new PostgreSqlSink(postgresOptions), batchingOptions);
return sinkConfiguration.Sink(batchingSink, restrictedToMinimumLevel, levelSwitch);

New code:

var batchingOptions = new BatchingOptions()
{
    BatchSizeLimit = postgresOptions.BatchSizeLimit,
    BufferingTimeLimit = postgresOptions.Period,
    QueueLimit = postgresOptions.QueueLimit
};

return sinkConfiguration.Sink(
    new PostgreSqlSink(postgresOptions), batchingOptions, restrictedToMinimumLevel, levelSwitch);

When you're done, don't forget to remove the Serilog.Sinks.PeriodicBatching package dependency.

Getting started

Sinks that, for performance reasons, need to emit events in batches, can be implemented using PeriodicBatchingSink from this package.

First, install the package into your Sink project:

dotnet add package Serilog.Sinks.PeriodicBatching

Then, instead of implementing Serilog's ILogEventSink, implement IBatchedLogEventSink in your sink class:

class ExampleBatchedSink : IBatchedLogEventSink
{
    public async Task EmitBatchAsync(IEnumerable<LogEvent> batch)
    {
        foreach (var logEvent in batch)
            Console.WriteLine(logEvent);
    }
    
    public Task OnEmptyBatchAsync() { }
}

Finally, in your sink's configuration method, construct a PeriodicBatchingSink that wraps your batched sink:

public static class LoggerSinkExampleConfiguration
{
    public static LoggerConfiguration Example(this LoggerSinkConfiguration loggerSinkConfiguration)
    {
        var exampleSink = new ExampleBatchedSink();
        
        var batchingOptions = new PeriodicBatchingSinkOptions
        {
            BatchSizeLimit = 100,
            Period = TimeSpan.FromSeconds(2),
            EagerlyEmitFirstEvent = true,
            QueueLimit = 10000
        };
        
        var batchingSink = new PeriodicBatchingSink(exampleSink, batchingOptions);
        
        return loggerSinkConfiguration.Sink(batchingSink);
    }
}

About

Infrastructure for Serilog sinks that process events in batches.

License:Apache License 2.0


Languages

Language:C# 95.8%Language:PowerShell 4.2%