|
1 |
| -# Serilog.Sinks.Async [](https://ci.appveyor.com/project/JezzSantos/serilog-sinks-async) |
2 |
| -An async Serilog sink |
| 1 | +# Serilog.Sinks.Async [](https://ci.appveyor.com/project/serilog/serilog-sinks-async) [](https://www.nuget.org/packages/Serilog.Sinks.Async) [](https://gitter.im/serilog/serilog) |
3 | 2 |
|
4 |
| -Use this buffered, async, delegating, sink to reduce the time it takes for your app to write your log events to your sinks. This sink can work with any `IEventLogSink` you use. |
| 3 | +An asynchronous wrapper for other [Serilog](https://serilog.net) sinks. Use this sink to reduce the overhead of logging calls by delegating work to a background thread. This is especially suited to non-batching sinks like the [File](https://github.com/serilog/serilog-sinks-file) and [RollingFile](https://github.com/serilog-serilog-sinks-rollingfile) sinks that may be affected by I/O bottlenecks. |
5 | 4 |
|
6 |
| -Especially suited to sinks that are either slow to write or have I/O bottlenecks (like http, databases, file writes etc.). |
7 |
| -This sink uses a separate thread pool thread to write to your sink, freeing up the calling thread to run in your app without having to wait. |
| 5 | +**Note:** many of the network-based sinks (_CouchDB_, _Elasticsearch_, _MongoDB_, _Seq_, _Splunk_...) already perform asychronous batching natively and do not benefit from this wrapper. |
8 | 6 |
|
9 |
| -Utilizes the producer/consumer pattern (using the TPL `BufferBlock<T>` class), where the calling thread produces log events (on your main thread), and the consumer runs on a thread pool thread consuming log events and writing them to your sink. |
| 7 | +### Getting started |
10 | 8 |
|
11 |
| -Install from NuGet: |
| 9 | +Install from [NuGet](https://nuget.org/packages/serilog.sinks.async): |
12 | 10 |
|
13 | 11 | ```powershell
|
14 |
| -Install-Package Serilog.Sinks.Async |
| 12 | +Install-Package Serilog.Sinks.Async -Pre |
15 | 13 | ```
|
16 | 14 |
|
17 |
| -Add this sink to your pipeline: |
| 15 | +Assuming you have already installed the target sink, such as the rolling file sink, move the wrapped sink's configuration within a `WriteTo.Async()` statement: |
18 | 16 |
|
19 | 17 | ```csharp
|
20 | 18 | Log.Logger = new LoggerConfiguration()
|
21 |
| - .WriteTo.Async(x => x.Sink(new YourSink())) |
| 19 | + .WriteTo.Async(a => a.RollingFile("logs/myapp-{Date}.txt")) |
22 | 20 | // Other logger configuration
|
23 | 21 | .CreateLogger()
|
| 22 | + |
| 23 | +Log.Information("This will be written to disk on the worker thread"); |
| 24 | + |
| 25 | +// At application shutdown |
| 26 | +Log.CloseAndFlush(); |
24 | 27 | ```
|
25 | 28 |
|
26 |
| -Now `YourSink` will write messages using another [thread pool] thread while your logging thread gets on with more important stuff. |
| 29 | +The wrapped sink (`RollingFile` in this case) will be invoked on a worker thread while your application's thread gets on with more important stuff. |
| 30 | + |
| 31 | +Because the memory buffer may contain events that have not yet been written to the target sink, it is important to call `Log.CloseAndFlush()` or `Logger.Dispose()` when the application exits. |
| 32 | + |
| 33 | +### Buffering |
27 | 34 |
|
28 |
| -If you think your code is producing log events faster than your sink can consume and write them, then the buffer is going to grow in memory, until you run out! |
29 |
| -Set a maximum size of the buffer so that your memory is not filled up. |
30 |
| -Buffered log events are then (async) postponed in your app thread until your sink catches up. |
| 35 | +This sink uses a separate worker thread to write to your sink, freeing up the calling thread to run in your app without having to wait. |
| 36 | + |
| 37 | +The default memory buffer feeding the worker thread is capped to 10,000 items, after which arriving events will be dropped. To increase or decrease this limit, specify it when configuring the async sink. |
31 | 38 |
|
32 | 39 | ```csharp
|
33 |
| -Log.Logger = new LoggerConfiguration() |
34 |
| - .WriteTo.Async(x => x.Sink(new YourSink), 500) //Max number of logevents to buffer in memory |
35 |
| - // Other logger configurationg |
36 |
| - .CreateLogger() |
| 40 | + // Reduce the buffer to 500 events |
| 41 | + .WriteTo.Async(a => a.RollingFile("logs/myapp-{Date}.txt"), 500) |
37 | 42 | ```
|
38 | 43 |
|
39 |
| -## About this Sink |
40 |
| -This sink was created by this conversation thread: https://github.com/serilog/serilog/issues/809 |
| 44 | +### XML `<appSettings>` and JSON configuration |
| 45 | + |
| 46 | +XML and JSON configuration support has not yet been added for this wrapper. |
| 47 | + |
| 48 | +### About this sink |
| 49 | + |
| 50 | +This sink was created following this conversation thread: https://github.com/serilog/serilog/issues/809. |
0 commit comments