Skip to content

Commit 076b999

Browse files
committed
Try to reconnect on SocketException
1 parent a97f744 commit 076b999

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ namespace Serilog.Sinks.Splunk
2727
/// </summary>
2828
public class UdpSink : ILogEventSink, IDisposable
2929
{
30-
private Socket _socket;
30+
private readonly SplunkUdpSinkConnectionInfo _connectionInfo;
3131
private readonly ITextFormatter _formatter;
32+
private Socket _socket;
3233
private bool disposedValue = false;
3334

3435
/// <summary>
@@ -37,70 +38,85 @@ public class UdpSink : ILogEventSink, IDisposable
3738
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
3839
/// <param name="formatProvider">Optional format provider</param>
3940
/// <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))
4443
{
45-
Connect(connectionInfo);
46-
_formatter = CreateDefaultFormatter(formatProvider, renderTemplate);
4744
}
4845

4946
/// <summary>
5047
/// Creates an instance of the Splunk UDP Sink.
5148
/// </summary>
5249
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
5350
/// <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)
5752
{
58-
Connect(connectionInfo);
53+
_connectionInfo = connectionInfo;
5954
_formatter = formatter;
55+
Connect();
6056
}
6157

62-
private void Connect(SplunkUdpSinkConnectionInfo connectionInfo)
58+
/// <inheritdoc/>
59+
protected virtual void Dispose(bool disposing)
6360
{
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+
}
6670
}
6771

68-
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
72+
/// <inheritdoc/>
73+
public void Dispose()
6974
{
70-
return new SplunkJsonFormatter(renderTemplate, formatProvider);
75+
Dispose(true);
7176
}
7277

7378
/// <inheritdoc/>
7479
public void Emit(LogEvent logEvent)
7580
{
76-
var sb = new StringBuilder();
81+
byte[] data = Convert(logEvent);
7782

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();
7899
using (var sw = new StringWriter(sb))
79100
_formatter.Format(logEvent, sw);
80-
81-
_socket.Send(Encoding.UTF8.GetBytes(sb.ToString()));
101+
return Encoding.UTF8.GetBytes(sb.ToString());
82102
}
83103

84-
/// <inheritdoc/>
85-
protected virtual void Dispose(bool disposing)
104+
private void Connect()
86105
{
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+
}
95109

96-
disposedValue = true;
97-
}
110+
private void DisposeSocket()
111+
{
112+
_socket?.Close();
113+
_socket?.Dispose();
114+
_socket = null;
98115
}
99116

100-
/// <inheritdoc/>
101-
public void Dispose()
117+
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
102118
{
103-
Dispose(true);
119+
return new SplunkJsonFormatter(renderTemplate, formatProvider);
104120
}
105121
}
106122
}

0 commit comments

Comments
 (0)