19
19
using System . Text ;
20
20
using Serilog . Core ;
21
21
using Serilog . Events ;
22
- using Serilog . Formatting . Json ;
22
+ using Serilog . Formatting ;
23
23
24
24
namespace Serilog . Sinks . Splunk
25
25
{
@@ -28,22 +28,41 @@ namespace Serilog.Sinks.Splunk
28
28
/// </summary>
29
29
public class UdpSink : ILogEventSink , IDisposable
30
30
{
31
- readonly Socket _socket ;
32
- readonly JsonFormatter _jsonFormatter ;
31
+ Socket _socket ;
32
+ readonly ITextFormatter _formatter ;
33
33
34
34
/// <summary>
35
- /// Creates an instance of the Splunk UDP Sink
35
+ /// Creates an instance of the Splunk TCP Sink.
36
36
/// </summary>
37
- /// <param name="hostAddress">The Splunk Host</param>
38
- /// <param name="port">The UDP port configured in Splunk</param>
37
+ /// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
39
38
/// <param name="formatProvider">Optional format provider</param>
40
39
/// <param name="renderTemplate">If true, the message template will be rendered</param>
41
- public UdpSink ( IPAddress hostAddress , int port , IFormatProvider formatProvider = null , bool renderTemplate = true )
40
+ public UdpSink (
41
+ SplunkUdpSinkConnectionInfo connectionInfo ,
42
+ IFormatProvider formatProvider = null ,
43
+ bool renderTemplate = true )
42
44
{
43
- _socket = new Socket ( SocketType . Dgram , ProtocolType . Udp ) ;
44
- _socket . Connect ( hostAddress , port ) ;
45
+ Connect ( connectionInfo ) ;
46
+ _formatter = CreateDefaultFormatter ( formatProvider , renderTemplate ) ;
47
+ }
45
48
46
- _jsonFormatter = new SplunkJsonFormatter ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
49
+ /// <summary>
50
+ /// Creates an instance of the Splunk TCP Sink.
51
+ /// </summary>
52
+ /// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
53
+ /// <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 )
57
+ {
58
+ Connect ( connectionInfo ) ;
59
+ _formatter = formatter ;
60
+ }
61
+
62
+ private void Connect ( SplunkUdpSinkConnectionInfo connectionInfo )
63
+ {
64
+ _socket = new Socket ( SocketType . Dgram , ProtocolType . Udp ) ;
65
+ _socket . Connect ( connectionInfo . Host , connectionInfo . Port ) ;
47
66
}
48
67
49
68
/// <summary>
@@ -53,34 +72,47 @@ public UdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider =
53
72
/// <param name="port">The UDP port configured in Splunk</param>
54
73
/// <param name="formatProvider">Optional format provider</param>
55
74
/// <param name="renderTemplate">If true, the message template is rendered</param>
56
- public UdpSink ( string host , int port , IFormatProvider formatProvider = null ,
57
- bool renderTemplate = true )
75
+ [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
76
+ public UdpSink ( string host , int port , IFormatProvider formatProvider = null , bool renderTemplate = true )
77
+ : this ( new SplunkUdpSinkConnectionInfo ( host , port ) , formatProvider , renderTemplate )
58
78
{
59
- _socket = new Socket ( SocketType . Dgram , ProtocolType . Udp ) ;
60
- _socket . Connect ( host , port ) ;
79
+ }
61
80
62
- _jsonFormatter = new SplunkJsonFormatter ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
81
+ /// <summary>
82
+ /// Creates an instance of the Splunk UDP Sink
83
+ /// </summary>
84
+ /// <param name="hostAddress">The Splunk Host</param>
85
+ /// <param name="port">The UDP port configured in Splunk</param>
86
+ /// <param name="formatProvider">Optional format provider</param>
87
+ /// <param name="renderTemplate">If true, the message template will be rendered</param>
88
+ [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
89
+ public UdpSink ( IPAddress hostAddress , int port , IFormatProvider formatProvider = null , bool renderTemplate = true )
90
+ : this ( new SplunkUdpSinkConnectionInfo ( hostAddress , port ) , formatProvider , renderTemplate )
91
+ {
92
+ }
93
+
94
+ private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
95
+ {
96
+ return new SplunkJsonFormatter ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
63
97
}
64
98
65
99
/// <inheritdoc/>
66
100
public void Emit ( LogEvent logEvent )
67
101
{
68
- var sw = new StringWriter ( ) ;
102
+ var sb = new StringBuilder ( ) ;
69
103
70
- _jsonFormatter . Format ( logEvent , sw ) ;
104
+ using ( var sw = new StringWriter ( sb ) )
105
+ _formatter . Format ( logEvent , sw ) ;
71
106
72
- var message = sw . ToString ( ) ;
73
-
74
- _socket . Send ( Encoding . UTF8 . GetBytes ( message ) ) ;
107
+ _socket . Send ( Encoding . UTF8 . GetBytes ( sb . ToString ( ) ) ) ;
75
108
}
76
109
77
110
/// <inheritdoc/>
78
111
public void Dispose ( )
79
112
{
80
- _socket . Close ( ) ;
81
- _socket . Dispose ( ) ;
113
+ _socket ? . Close ( ) ;
114
+ _socket ? . Dispose ( ) ;
115
+ _socket = null ;
82
116
}
83
117
}
84
118
}
85
-
86
-
0 commit comments