https://github.com/serilog/serilog-sinks-periodicbatching
Infrastructure for Serilog sinks that process events in batches.
https://github.com/serilog/serilog-sinks-periodicbatching
Last synced: 2 months ago
JSON representation
Infrastructure for Serilog sinks that process events in batches.
- Host: GitHub
- URL: https://github.com/serilog/serilog-sinks-periodicbatching
- Owner: serilog
- License: apache-2.0
- Created: 2016-03-09T06:02:17.000Z (over 9 years ago)
- Default Branch: dev
- Last Pushed: 2024-07-02T21:10:11.000Z (12 months ago)
- Last Synced: 2025-04-11T10:05:59.105Z (3 months ago)
- Language: C#
- Homepage:
- Size: 220 KB
- Stars: 74
- Watchers: 14
- Forks: 28
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# Serilog.Sinks.PeriodicBatching [](https://ci.appveyor.com/project/serilog/serilog-sinks-periodicbatching) [](https://www.nuget.org/packages/Serilog.Sinks.periodicbatching/)
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](https://github.com/serilog-contrib/Serilog.Sinks.Postgresql.Alternative/commit/dd282cdd01c694ed586052a023e653d11dda62d1). Old code:
```csharp
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:
```csharp
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:
```csharp
class ExampleBatchedSink : IBatchedLogEventSink
{
public async Task EmitBatchAsync(IEnumerable 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:
```csharp
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);
}
}
```