16
16
using Serilog . Events ;
17
17
using Serilog . Formatting ;
18
18
using System ;
19
+ using System . Collections . Generic ;
19
20
using System . IO ;
20
21
using System . Net . Sockets ;
21
22
using System . Text ;
23
+ using System . Threading . Tasks ;
24
+ using Serilog . Sinks . PeriodicBatching ;
22
25
23
26
namespace Serilog . Sinks . Splunk
24
27
{
25
28
/// <summary>
26
29
/// A sink that logs to Splunk over UDP
27
30
/// </summary>
28
- public class UdpSink : ILogEventSink , IDisposable
31
+ public class UdpSink : PeriodicBatchingSink
29
32
{
30
33
private readonly SplunkUdpSinkConnectionInfo _connectionInfo ;
31
34
private readonly ITextFormatter _formatter ;
32
35
private Socket _socket ;
33
- private bool disposedValue = false ;
34
36
35
37
/// <summary>
36
38
/// Creates an instance of the Splunk UDP Sink.
@@ -48,51 +50,15 @@ public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, IFormatProvider forma
48
50
/// </summary>
49
51
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
50
52
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
51
- public UdpSink ( SplunkUdpSinkConnectionInfo connectionInfo , ITextFormatter formatter )
53
+ public UdpSink ( SplunkUdpSinkConnectionInfo connectionInfo , ITextFormatter formatter )
54
+ : base ( connectionInfo . BatchPostingLimit , connectionInfo . Period , connectionInfo . QueueSizeLimit )
52
55
{
53
56
_connectionInfo = connectionInfo ;
54
57
_formatter = formatter ;
55
58
Connect ( ) ;
56
59
}
57
-
58
- /// <inheritdoc/>
59
- protected virtual void Dispose ( bool disposing )
60
- {
61
- if ( ! disposedValue )
62
- {
63
- if ( disposing )
64
- {
65
- DisposeSocket ( ) ;
66
- }
67
-
68
- disposedValue = true ;
69
- }
70
- }
71
-
72
- /// <inheritdoc/>
73
- public void Dispose ( )
74
- {
75
- Dispose ( true ) ;
76
- }
77
-
78
- /// <inheritdoc/>
79
- public void Emit ( LogEvent logEvent )
80
- {
81
- byte [ ] data = Convert ( logEvent ) ;
82
-
83
- try
84
- {
85
- _socket . Send ( data ) ;
86
- }
87
- catch ( SocketException )
88
- {
89
- // Try to reconnect and log
90
- DisposeSocket ( ) ;
91
- Connect ( ) ;
92
- _socket . Send ( data ) ;
93
- }
94
- }
95
-
60
+
61
+
96
62
private byte [ ] Convert ( LogEvent logEvent )
97
63
{
98
64
var sb = new StringBuilder ( ) ;
@@ -107,13 +73,51 @@ private void Connect()
107
73
_socket . Connect ( _connectionInfo . Host , _connectionInfo . Port ) ;
108
74
}
109
75
76
+
77
+ protected override void Dispose ( bool disposing )
78
+ {
79
+ DisposeSocket ( ) ;
80
+
81
+ base . Dispose ( true ) ;
82
+ }
83
+
110
84
private void DisposeSocket ( )
111
85
{
112
86
_socket ? . Close ( ) ;
113
87
_socket ? . Dispose ( ) ;
114
88
_socket = null ;
115
89
}
116
90
91
+ /// <summary>
92
+ /// Emit a batch of log events, running to completion synchronously.
93
+ /// </summary>
94
+ /// <param name="events">The events to emit.</param>
95
+ /// <remarks>
96
+ /// Override either <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
97
+ /// or <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />,
98
+ /// not both.
99
+ /// </remarks>
100
+ protected override async Task EmitBatchAsync ( IEnumerable < LogEvent > events )
101
+ {
102
+ foreach ( var logEvent in events )
103
+ {
104
+ byte [ ] data = Convert ( logEvent ) ;
105
+
106
+ try
107
+ {
108
+ _socket . Send ( data ) ;
109
+ }
110
+ catch ( SocketException )
111
+ {
112
+ // Try to reconnect and log
113
+ DisposeSocket ( ) ;
114
+ Connect ( ) ;
115
+ _socket . Send ( data ) ;
116
+ }
117
+ }
118
+ }
119
+
120
+
117
121
private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
118
122
{
119
123
return new SplunkJsonFormatter ( renderTemplate , formatProvider ) ;
0 commit comments