Serilog.Sinks.PeriodicBatching 5.0.1-dev-00860

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);
    }
}

Showing the top 20 packages that depend on Serilog.Sinks.PeriodicBatching.

Packages Downloads
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
33
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
35
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
36
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
38
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
41
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
43
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
48
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
77
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
32
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
33
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
35
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
36
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
38
Serilog.Sinks.PostgreSQL
Serilog sink for writing to PostgreSQL table
37
Serilog.Sinks.PostgreSQL
Serilog sink for writing to PostgreSQL table
39
Serilog.Sinks.PostgreSQL
Serilog sink for writing to PostgreSQL table
207

.NET Framework 4.6.2

.NET Framework 4.7.1

.NET 6.0

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
5.0.1-dev-00860 29 07/18/2024
5.0.0 39 07/22/2024
5.0.0-dev-00855 31 07/23/2024
4.1.2-dev-00851 27 07/23/2024
4.1.1 35 07/23/2024
4.1.1-dev-00845 29 07/23/2024
4.1.0 31 07/23/2024
4.1.0-dev-00840 33 07/23/2024
4.0.2-dev-00838 29 07/23/2024
4.0.1 35 07/23/2024
4.0.1-dev-00832 27 07/23/2024
4.0.0 41 07/23/2024
4.0.0-dev-00824 28 07/23/2024
4.0.0-dev-00821 26 07/23/2024
3.1.1-dev-00804 33 07/21/2024
3.1.1-dev-00801 31 07/23/2024
3.1.0 35 07/23/2024
3.1.0-dev-00796 29 07/23/2024
3.0.0 42 07/23/2024
3.0.0-dev-00792 51 07/23/2024
2.3.1 25 07/23/2024
2.3.1-dev-00785 26 07/23/2024
2.3.1-dev-00780 40 07/23/2024
2.3.1-dev-00776 23 07/23/2024
2.3.1-dev-00774 42 07/23/2024
2.3.0 115 02/04/2024
2.3.0-dev-00765 32 07/23/2024
2.3.0-dev-00762 22 07/23/2024
2.3.0-dev-00760 30 07/23/2024
2.2.1-dev-00758 30 07/23/2024
2.2.1-dev-00755 30 07/23/2024
2.2.0 34 07/23/2024
2.2.0-dev-00748 26 07/23/2024
2.2.0-dev-00740 36 07/22/2024
2.1.1 35 07/21/2024
2.1.1-dev-00732 32 07/23/2024
2.1.1-dev-00729 30 07/23/2024
2.1.1-dev-00725 27 07/23/2024
2.1.0 38 07/23/2024
2.1.0-dev-00720 22 07/23/2024
2.0.2-dev-00719 28 07/23/2024
2.0.1 30 07/23/2024
2.0.1-dev-00714 25 07/23/2024
2.0.0 23 07/23/2024
2.0.0-rc-707 34 07/23/2024
2.0.0-rc-705 28 07/18/2024
2.0.0-beta-702 26 07/18/2024
2.0.0-beta-700 30 07/18/2024
2.0.0-beta-519 31 07/18/2024
2.0.0-beta-516 33 07/18/2024
2.0.0-beta-513 21 07/18/2024
2.0.0-beta-511 22 07/18/2024
2.0.0-beta-509 32 07/18/2024
2.0.0-beta-507 28 07/18/2024
2.0.0-beta-505 34 07/18/2024
2.0.0-beta-502 31 07/18/2024
2.0.0-beta-499 34 07/18/2024
2.0.0-beta-495 22 07/18/2024
2.0.0-beta-494 28 07/18/2024
2.0.0-beta-493 26 07/18/2024
2.0.0-beta-487 30 07/18/2024
2.0.0-beta-486 32 07/18/2024
2.0.0-beta-479 25 07/18/2024
2.0.0-beta-478 28 07/18/2024
2.0.0-beta-465 28 07/18/2024