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.AmazonKinesis
Serilog event sink that writes to Amazon Kinesis.
157
Serilog.Sinks.AwsCloudWatch
A Serilog sink that logs to AWS CloudWatch
64
Serilog.Sinks.Elasticsearch
Package Description
54
Serilog.Sinks.AwsCloudWatch
A Serilog sink that logs to AWS CloudWatch
52
Serilog.Sinks.Elasticsearch
Serilog sink for Elasticsearch
48
Serilog.Sinks.Elasticsearch
Package Description
47
Serilog.Sinks.Elasticsearch
Serilog sink for Elasticsearch
45
Serilog.Sinks.Elasticsearch
Package Description
44
Serilog.Sinks.Elasticsearch
Package Description
43
Serilog.Sinks.Elasticsearch
Package Description
42
Serilog.Sinks.Elasticsearch
Serilog sink for Elasticsearch
41
Serilog.Sinks.Elasticsearch
Serilog.Sinks.Elasticsearch
41
Serilog.Sinks.Elasticsearch
Package Description
40
Serilog.Sinks.AwsCloudWatch
A Serilog sink that logs to AWS CloudWatch
40
Serilog.Sinks.Elasticsearch
Package Description
39
Serilog.Sinks.MSSqlServerCore
DEPRECATED - Please use official Serilog sink for MSSqlServer as it now fully supports .NET Standard
38
Serilog.Sinks.AwsCloudWatch
A Serilog sink that logs to AWS CloudWatch
38

.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 17 07/06/2024
5.0.0 10 06/07/2024
5.0.0-dev-00855 12 06/08/2024
4.1.2-dev-00851 13 06/01/2024
4.1.1 29 05/31/2024
4.1.1-dev-00845 14 05/24/2024
4.1.0 31 05/15/2024
4.1.0-dev-00840 18 05/16/2024
4.0.2-dev-00838 15 05/16/2024
4.0.1 22 03/18/2024
4.0.1-dev-00832 14 03/19/2024
4.0.0 18 03/18/2024
4.0.0-dev-00824 19 02/09/2024
4.0.0-dev-00821 19 02/09/2024
3.1.1-dev-00804 28 06/02/2023
3.1.1-dev-00801 15 08/30/2023
3.1.0 18 08/24/2023
3.1.0-dev-00796 59 06/01/2023
3.0.0 35 08/24/2023
3.0.0-dev-00792 16 08/30/2023
2.3.1 20 08/31/2023
2.3.1-dev-00785 19 08/26/2023
2.3.1-dev-00780 18 08/30/2023
2.3.1-dev-00776 18 09/09/2023
2.3.1-dev-00774 17 08/27/2023
2.3.0 20 08/25/2023
2.3.0-dev-00765 13 08/31/2023
2.3.0-dev-00762 25 08/25/2023
2.3.0-dev-00760 14 08/30/2023
2.2.1-dev-00758 14 05/18/2023
2.2.1-dev-00755 14 08/26/2023
2.2.0 21 08/28/2023
2.2.0-dev-00748 25 08/25/2023
2.2.0-dev-00740 25 08/30/2023
2.1.1 79 07/01/2020
2.1.1-dev-00732 15 08/26/2023
2.1.1-dev-00729 16 08/30/2023
2.1.1-dev-00725 14 02/15/2024
2.1.0 142 05/07/2021
2.1.0-dev-00720 21 08/27/2023
2.0.2-dev-00719 48 05/17/2023
2.0.1 13 08/29/2023
2.0.1-dev-00714 18 08/30/2023
2.0.0 42 11/25/2019
2.0.0-rc-707 19 08/30/2023
2.0.0-rc-705 39 08/25/2023
2.0.0-beta-702 28 08/29/2023
2.0.0-beta-700 19 08/27/2023
2.0.0-beta-519 19 08/27/2023
2.0.0-beta-516 16 08/31/2023
2.0.0-beta-513 25 08/26/2023
2.0.0-beta-511 22 08/28/2023
2.0.0-beta-509 29 08/25/2023
2.0.0-beta-507 20 05/18/2023
2.0.0-beta-505 16 09/01/2023
2.0.0-beta-502 16 08/27/2023
2.0.0-beta-499 14 08/29/2023
2.0.0-beta-495 17 08/26/2023
2.0.0-beta-494 19 12/07/2023
2.0.0-beta-493 14 08/28/2023
2.0.0-beta-487 23 08/25/2023
2.0.0-beta-486 16 05/17/2023
2.0.0-beta-479 20 08/31/2023
2.0.0-beta-478 24 08/30/2023
2.0.0-beta-465 16 08/26/2023