1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
3
using System . Threading ;
4
+ using System . Threading . Tasks ;
4
5
using Serilog . Core ;
5
6
using Serilog . Debugging ;
6
7
using Serilog . Events ;
7
- using System . Threading . Tasks ;
8
8
9
9
namespace Serilog . Sinks . Async
10
10
{
11
11
sealed class BackgroundWorkerSink : ILogEventSink , IDisposable
12
12
{
13
13
readonly ILogEventSink _pipeline ;
14
- readonly int _bufferCapacity ;
15
14
readonly bool _blockWhenFull ;
16
15
readonly BlockingCollection < LogEvent > _queue ;
17
16
readonly Task _worker ;
@@ -21,15 +20,14 @@ public BackgroundWorkerSink(ILogEventSink pipeline, int bufferCapacity, bool blo
21
20
if ( pipeline == null ) throw new ArgumentNullException ( nameof ( pipeline ) ) ;
22
21
if ( bufferCapacity <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( bufferCapacity ) ) ;
23
22
_pipeline = pipeline ;
24
- _bufferCapacity = bufferCapacity ;
25
23
_blockWhenFull = blockWhenFull ;
26
- _queue = new BlockingCollection < LogEvent > ( _bufferCapacity ) ;
24
+ _queue = new BlockingCollection < LogEvent > ( bufferCapacity ) ;
27
25
_worker = Task . Factory . StartNew ( Pump , CancellationToken . None , TaskCreationOptions . LongRunning | TaskCreationOptions . DenyChildAttach , TaskScheduler . Default ) ;
28
26
}
29
27
30
28
public void Emit ( LogEvent logEvent )
31
29
{
32
- if ( this . _queue . IsAddingCompleted )
30
+ if ( _queue . IsAddingCompleted )
33
31
return ;
34
32
35
33
try
@@ -41,12 +39,12 @@ public void Emit(LogEvent logEvent)
41
39
else
42
40
{
43
41
if ( ! _queue . TryAdd ( logEvent ) )
44
- SelfLog . WriteLine ( "{0} unable to enqueue, capacity {1}" , typeof ( BackgroundWorkerSink ) , _bufferCapacity ) ;
42
+ SelfLog . WriteLine ( "{0} unable to enqueue, capacity {1}" , typeof ( BackgroundWorkerSink ) , _queue . BoundedCapacity ) ;
45
43
}
46
44
}
47
45
catch ( InvalidOperationException )
48
46
{
49
- // Thrown in the event of a race condition when we try to add another event after
47
+ // Thrown in the event of a race condition when we try to add another event after
50
48
// CompleteAdding has been called
51
49
}
52
50
}
0 commit comments