1818using System . Linq ;
1919using System . Threading . Tasks ;
2020using Serilog . Events ;
21+ using Serilog . Formatting ;
2122using Serilog . Sinks . PeriodicBatching ;
2223using Serilog . Sinks . Seq . Http ;
2324
24- namespace Serilog . Sinks . Seq . Batched
25+ namespace Serilog . Sinks . Seq . Batched ;
26+
27+ /// <summary>
28+ /// The default Seq sink, for use in combination with <see cref="PeriodicBatchingSink"/>.
29+ /// </summary>
30+ sealed class BatchedSeqSink : IBatchedLogEventSink , IDisposable
2531{
26- /// <summary>
27- /// The default Seq sink, for use in combination with <see cref="PeriodicBatchingSink"/>.
28- /// </summary>
29- sealed class BatchedSeqSink : IBatchedLogEventSink , IDisposable
30- {
31- static readonly TimeSpan RequiredLevelCheckInterval = TimeSpan . FromMinutes ( 2 ) ;
32+ static readonly TimeSpan RequiredLevelCheckInterval = TimeSpan . FromMinutes ( 2 ) ;
3233
33- readonly ConstrainedBufferedFormatter _formatter ;
34- readonly SeqIngestionApi _ingestionApi ;
34+ readonly ConstrainedBufferedFormatter _payloadFormatter ;
35+ readonly SeqIngestionApi _ingestionApi ;
3536
36- DateTime _nextRequiredLevelCheckUtc = DateTime . UtcNow . Add ( RequiredLevelCheckInterval ) ;
37- readonly ControlledLevelSwitch _controlledSwitch ;
37+ DateTime _nextRequiredLevelCheckUtc = DateTime . UtcNow . Add ( RequiredLevelCheckInterval ) ;
38+ readonly ControlledLevelSwitch _controlledSwitch ;
3839
39- public BatchedSeqSink (
40- SeqIngestionApi ingestionApi ,
41- long ? eventBodyLimitBytes ,
42- ControlledLevelSwitch controlledSwitch )
43- {
44- _controlledSwitch = controlledSwitch ?? throw new ArgumentNullException ( nameof ( controlledSwitch ) ) ;
45- _formatter = new ConstrainedBufferedFormatter ( eventBodyLimitBytes ) ;
46- _ingestionApi = ingestionApi ?? throw new ArgumentNullException ( nameof ( ingestionApi ) ) ;
47- }
40+ public BatchedSeqSink (
41+ SeqIngestionApi ingestionApi ,
42+ ITextFormatter payloadFormatter ,
43+ long ? eventBodyLimitBytes ,
44+ ControlledLevelSwitch controlledSwitch )
45+ {
46+ _controlledSwitch = controlledSwitch ?? throw new ArgumentNullException ( nameof ( controlledSwitch ) ) ;
47+ _payloadFormatter = new ConstrainedBufferedFormatter ( eventBodyLimitBytes , payloadFormatter ) ;
48+ _ingestionApi = ingestionApi ?? throw new ArgumentNullException ( nameof ( ingestionApi ) ) ;
49+ }
4850
49- public void Dispose ( )
50- {
51- _ingestionApi . Dispose ( ) ;
52- }
51+ public void Dispose ( )
52+ {
53+ _ingestionApi . Dispose ( ) ;
54+ }
5355
54- // The sink must emit at least one event on startup, and the server be
55- // configured to set a specific level, before background level checks will be performed.
56- public async Task OnEmptyBatchAsync ( )
56+ // The sink must emit at least one event on startup, and the server be
57+ // configured to set a specific level, before background level checks will be performed.
58+ public async Task OnEmptyBatchAsync ( )
59+ {
60+ if ( _controlledSwitch . IsActive &&
61+ _nextRequiredLevelCheckUtc < DateTime . UtcNow )
5762 {
58- if ( _controlledSwitch . IsActive &&
59- _nextRequiredLevelCheckUtc < DateTime . UtcNow )
60- {
61- await EmitBatchAsync ( Enumerable . Empty < LogEvent > ( ) ) ;
62- }
63+ await EmitBatchAsync ( Enumerable . Empty < LogEvent > ( ) ) ;
6364 }
65+ }
6466
65- public async Task EmitBatchAsync ( IEnumerable < LogEvent > events )
66- {
67- _nextRequiredLevelCheckUtc = DateTime . UtcNow . Add ( RequiredLevelCheckInterval ) ;
67+ public async Task EmitBatchAsync ( IEnumerable < LogEvent > events )
68+ {
69+ _nextRequiredLevelCheckUtc = DateTime . UtcNow . Add ( RequiredLevelCheckInterval ) ;
6870
69- var payload = new StringWriter ( ) ;
70- foreach ( var evt in events )
71- {
72- _formatter . Format ( evt , payload ) ;
73- }
71+ var payload = new StringWriter ( ) ;
72+ foreach ( var evt in events )
73+ {
74+ _payloadFormatter . Format ( evt , payload ) ;
75+ }
7476
75- var clefPayload = payload . ToString ( ) ;
77+ var clefPayload = payload . ToString ( ) ;
7678
77- var minimumAcceptedLevel = await _ingestionApi . IngestAsync ( clefPayload ) ;
79+ var minimumAcceptedLevel = await _ingestionApi . IngestAsync ( clefPayload ) ;
7880
79- _controlledSwitch . Update ( minimumAcceptedLevel ) ;
80- }
81+ _controlledSwitch . Update ( minimumAcceptedLevel ) ;
8182 }
82- }
83+ }
0 commit comments