@@ -27,8 +27,9 @@ namespace Serilog.Sinks.Splunk
27
27
/// </summary>
28
28
public class UdpSink : ILogEventSink , IDisposable
29
29
{
30
- private Socket _socket ;
30
+ private readonly SplunkUdpSinkConnectionInfo _connectionInfo ;
31
31
private readonly ITextFormatter _formatter ;
32
+ private Socket _socket ;
32
33
private bool disposedValue = false ;
33
34
34
35
/// <summary>
@@ -37,70 +38,85 @@ public class UdpSink : ILogEventSink, IDisposable
37
38
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
38
39
/// <param name="formatProvider">Optional format provider</param>
39
40
/// <param name="renderTemplate">If true, the message template will be rendered</param>
40
- public UdpSink (
41
- SplunkUdpSinkConnectionInfo connectionInfo ,
42
- IFormatProvider formatProvider = null ,
43
- bool renderTemplate = true )
41
+ public UdpSink ( SplunkUdpSinkConnectionInfo connectionInfo , IFormatProvider formatProvider = null , bool renderTemplate = true )
42
+ : this ( connectionInfo , CreateDefaultFormatter ( formatProvider , renderTemplate ) )
44
43
{
45
- Connect ( connectionInfo ) ;
46
- _formatter = CreateDefaultFormatter ( formatProvider , renderTemplate ) ;
47
44
}
48
45
49
46
/// <summary>
50
47
/// Creates an instance of the Splunk UDP Sink.
51
48
/// </summary>
52
49
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
53
50
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
54
- public UdpSink (
55
- SplunkUdpSinkConnectionInfo connectionInfo ,
56
- ITextFormatter formatter )
51
+ public UdpSink ( SplunkUdpSinkConnectionInfo connectionInfo , ITextFormatter formatter )
57
52
{
58
- Connect ( connectionInfo ) ;
53
+ _connectionInfo = connectionInfo ;
59
54
_formatter = formatter ;
55
+ Connect ( ) ;
60
56
}
61
57
62
- private void Connect ( SplunkUdpSinkConnectionInfo connectionInfo )
58
+ /// <inheritdoc/>
59
+ protected virtual void Dispose ( bool disposing )
63
60
{
64
- _socket = new Socket ( SocketType . Dgram , ProtocolType . Udp ) ;
65
- _socket . Connect ( connectionInfo . Host , connectionInfo . Port ) ;
61
+ if ( ! disposedValue )
62
+ {
63
+ if ( disposing )
64
+ {
65
+ DisposeSocket ( ) ;
66
+ }
67
+
68
+ disposedValue = true ;
69
+ }
66
70
}
67
71
68
- private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
72
+ /// <inheritdoc/>
73
+ public void Dispose ( )
69
74
{
70
- return new SplunkJsonFormatter ( renderTemplate , formatProvider ) ;
75
+ Dispose ( true ) ;
71
76
}
72
77
73
78
/// <inheritdoc/>
74
79
public void Emit ( LogEvent logEvent )
75
80
{
76
- var sb = new StringBuilder ( ) ;
81
+ byte [ ] data = Convert ( logEvent ) ;
77
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
+
96
+ private byte [ ] Convert ( LogEvent logEvent )
97
+ {
98
+ var sb = new StringBuilder ( ) ;
78
99
using ( var sw = new StringWriter ( sb ) )
79
100
_formatter . Format ( logEvent , sw ) ;
80
-
81
- _socket . Send ( Encoding . UTF8 . GetBytes ( sb . ToString ( ) ) ) ;
101
+ return Encoding . UTF8 . GetBytes ( sb . ToString ( ) ) ;
82
102
}
83
103
84
- /// <inheritdoc/>
85
- protected virtual void Dispose ( bool disposing )
104
+ private void Connect ( )
86
105
{
87
- if ( ! disposedValue )
88
- {
89
- if ( disposing )
90
- {
91
- _socket ? . Close ( ) ;
92
- _socket ? . Dispose ( ) ;
93
- _socket = null ;
94
- }
106
+ _socket = new Socket ( SocketType . Dgram , ProtocolType . Udp ) ;
107
+ _socket . Connect ( _connectionInfo . Host , _connectionInfo . Port ) ;
108
+ }
95
109
96
- disposedValue = true ;
97
- }
110
+ private void DisposeSocket ( )
111
+ {
112
+ _socket ? . Close ( ) ;
113
+ _socket ? . Dispose ( ) ;
114
+ _socket = null ;
98
115
}
99
116
100
- /// <inheritdoc/>
101
- public void Dispose ( )
117
+ private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
102
118
{
103
- Dispose ( true ) ;
119
+ return new SplunkJsonFormatter ( renderTemplate , formatProvider ) ;
104
120
}
105
121
}
106
122
}
0 commit comments