|
1 |
| -# Serilog.Sinks.Async [](https://ci.appveyor.com/project/serilog/serilog-sinks-async) [](https://www.nuget.org/packages/Serilog.Sinks.Async) |
| 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) |
2 | 2 |
|
3 |
| -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 `ILogEventSink` 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. |
4 | 4 |
|
5 |
| -Especially suited to non-batching sinks that are either slow to write or have I/O bottlenecks (like http, databases, file writes etc.). |
| 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. |
6 | 6 |
|
7 |
| -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. |
| 7 | +### Getting started |
8 | 8 |
|
9 |
| -Install from NuGet: |
| 9 | +Install from [NuGet](https://nuget.org/packages/serilog.sinks.async): |
10 | 10 |
|
11 | 11 | ```powershell
|
12 | 12 | Install-Package Serilog.Sinks.Async -Pre
|
13 | 13 | ```
|
14 | 14 |
|
15 |
| -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: |
16 | 16 |
|
17 | 17 | ```csharp
|
18 | 18 | Log.Logger = new LoggerConfiguration()
|
19 |
| - .WriteTo.Async(x => x.YourSink()) |
| 19 | + .WriteTo.Async(a => a.RollingFile("logs/myapp-{Date}.txt")) |
20 | 20 | // Other logger configuration
|
21 | 21 | .CreateLogger()
|
| 22 | + |
| 23 | +Log.Information("This will be writtend to disk on the worker thread"); |
| 24 | + |
| 25 | +// At application shutdown |
| 26 | +Log.CloseAndFlush(); |
22 | 27 | ```
|
23 | 28 |
|
24 |
| -Now `YourSink` will write messages using a worker thread while your applicatoin 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 |
| 34 | + |
| 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. |
25 | 36 |
|
26 | 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.
|
27 | 38 |
|
28 | 39 | ```csharp
|
29 |
| -Log.Logger = new LoggerConfiguration() |
30 |
| - .WriteTo.Async(x => x.YourSink(), 500) // Max number of events to buffer in memory |
31 |
| - // Other logger configurationg |
32 |
| - .CreateLogger() |
| 40 | + // Reduce the buffer to 500 events |
| 41 | + .WriteTo.Async(a => a.RollingFile("logs/myapp-{Date}.txt"), 500) |
33 | 42 | ```
|
34 | 43 |
|
35 |
| -## About this sink |
| 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 |
36 | 49 |
|
37 |
| -This sink was created following this conversation thread: https://github.com/serilog/serilog/issues/809 |
| 50 | +This sink was created following this conversation thread: https://github.com/serilog/serilog/issues/809. |
0 commit comments