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.
66
Microsoft.Extensions.Logging.AzureAppServices
Logger implementation to support Azure App Services 'Diagnostics logs' and 'Log stream' features.
66
Microsoft.Extensions.Logging.AzureAppServices
Logger implementation to support Azure App Services 'Diagnostics logs' and 'Log stream' features.
64
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
63
Serilog.Sinks.Seq
Serilog sink that writes to the Seq log server over HTTP/HTTPS.
62
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
61
Serilog.Sinks.Seq
Serilog sink that writes to the Seq log server over HTTP/HTTPS.
60
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
60
Serilog.Sinks.Seq
Serilog sink that writes to the Seq event server over HTTP/S.
60
Microsoft.Extensions.Logging.AzureAppServices
Logger implementation to support Azure App Services 'Diagnostics logs' and 'Log stream' features.
60
Serilog.Sinks.Seq
Serilog sink that writes to the Seq log server over HTTP/S.
59
Serilog.Sinks.Seq
Serilog sink that writes to the Seq event server over HTTP/S.
59
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
59
Microsoft.Extensions.Logging.AzureAppServices
Logger implementation to support Azure App Services 'Diagnostics logs' and 'Log stream' features.
59
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
58
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
58
Serilog.Sinks.Http
A Serilog sink sending log events over HTTP.
57
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
57
Serilog.Sinks.Seq
Serilog sink that writes to the Seq event server over HTTP/S.
56
Serilog.Sinks.Http
Serilog event sink that sends HTTP POST requests over the network.
56

.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 47 07/03/2024
5.0.0 47 06/07/2024
5.0.0-dev-00855 40 06/08/2024
4.1.2-dev-00851 49 06/04/2024
4.1.1 40 06/02/2024
4.1.1-dev-00845 43 06/04/2024
4.1.0 44 05/06/2024
4.1.0-dev-00840 45 05/07/2024
4.0.2-dev-00838 44 05/07/2024
4.0.1 52 03/21/2024
4.0.1-dev-00832 47 03/22/2024
4.0.0 48 02/23/2024
4.0.0-dev-00824 47 02/25/2024
4.0.0-dev-00821 50 01/12/2024
3.1.1-dev-00804 54 07/21/2023
3.1.1-dev-00801 54 02/17/2023
3.1.0 52 08/09/2023
3.1.0-dev-00796 50 08/25/2023
3.0.0 51 02/17/2023
3.0.0-dev-00792 51 02/17/2023
2.3.1 57 09/18/2023
2.3.1-dev-00785 49 09/22/2023
2.3.1-dev-00780 48 08/04/2023
2.3.1-dev-00776 47 08/04/2023
2.3.1-dev-00774 53 09/22/2023
2.3.0 51 09/04/2023
2.3.0-dev-00765 49 07/23/2023
2.3.0-dev-00762 53 09/22/2023
2.3.0-dev-00760 50 12/02/2022
2.2.1-dev-00758 47 08/10/2023
2.2.1-dev-00755 55 07/11/2023
2.2.0 49 07/03/2022
2.2.0-dev-00748 45 09/02/2023
2.2.0-dev-00740 49 09/22/2023
2.1.1 53 08/09/2023
2.1.1-dev-00732 49 12/30/2022
2.1.1-dev-00729 44 09/15/2023
2.1.1-dev-00725 54 08/09/2023
2.1.0 55 09/28/2023
2.1.0-dev-00720 47 08/15/2023
2.0.2-dev-00719 49 09/21/2023
2.0.1 49 02/17/2023
2.0.1-dev-00714 49 09/22/2023
2.0.0 48 11/23/2022
2.0.0-rc-707 52 09/22/2023
2.0.0-rc-705 50 10/24/2022
2.0.0-beta-702 52 10/07/2022
2.0.0-beta-700 53 09/21/2023
2.0.0-beta-519 55 10/21/2022
2.0.0-beta-516 57 09/22/2023
2.0.0-beta-513 47 10/06/2022
2.0.0-beta-511 44 09/28/2023
2.0.0-beta-509 55 10/06/2022
2.0.0-beta-507 51 09/22/2023
2.0.0-beta-505 53 08/22/2023
2.0.0-beta-502 46 09/21/2023
2.0.0-beta-499 51 08/24/2023
2.0.0-beta-495 49 09/21/2023
2.0.0-beta-494 58 09/21/2023
2.0.0-beta-493 55 11/04/2022
2.0.0-beta-487 51 09/21/2023
2.0.0-beta-486 61 09/09/2023
2.0.0-beta-479 64 08/11/2023
2.0.0-beta-478 45 08/19/2023
2.0.0-beta-465 52 07/29/2023