15
15
using System ;
16
16
using System . IO ;
17
17
using System . Net ;
18
- using System . Net . Sockets ;
19
18
using System . Text ;
20
19
using Serilog . Core ;
21
20
using Serilog . Events ;
22
- using Serilog . Formatting . Json ;
21
+ using Serilog . Formatting ;
23
22
using Splunk . Logging ;
24
23
25
24
namespace Serilog . Sinks . Splunk
@@ -29,67 +28,79 @@ namespace Serilog.Sinks.Splunk
29
28
/// </summary>
30
29
public class TcpSink : ILogEventSink , IDisposable
31
30
{
32
- readonly JsonFormatter _jsonFormatter ;
31
+ readonly ITextFormatter _formatter ;
33
32
private TcpSocketWriter _writer ;
34
33
35
34
/// <summary>
36
35
/// Creates an instance of the Splunk TCP Sink
37
36
/// </summary>
38
- /// <param name="hostAddress ">The Splunk Host</param>
37
+ /// <param name="host ">The Splunk Host</param>
39
38
/// <param name="port">The UDP port configured in Splunk</param>
40
39
/// <param name="formatProvider">Optional format provider</param>
41
40
/// <param name="renderTemplate">If true, the message template will be rendered</param>
42
-
43
41
public TcpSink (
44
- IPAddress hostAddress ,
42
+ string host ,
45
43
int port ,
46
44
IFormatProvider formatProvider = null ,
47
- bool renderTemplate = true )
45
+ bool renderTemplate = true ) : this ( IPAddress . Parse ( host ) , port , formatProvider , renderTemplate )
48
46
{
49
- var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy ( ) ;
50
-
51
- _writer = new TcpSocketWriter ( hostAddress , port , reconnectionPolicy , 10000 ) ;
52
-
53
- _jsonFormatter = new SplunkJsonFormatter ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
54
47
}
55
48
56
49
/// <summary>
57
50
/// Creates an instance of the Splunk TCP Sink
58
51
/// </summary>
59
- /// <param name="host ">The Splunk Host</param>
52
+ /// <param name="hostAddress ">The Splunk Host</param>
60
53
/// <param name="port">The UDP port configured in Splunk</param>
61
54
/// <param name="formatProvider">Optional format provider</param>
62
55
/// <param name="renderTemplate">If true, the message template will be rendered</param>
63
56
public TcpSink (
64
- string host ,
57
+ IPAddress hostAddress ,
65
58
int port ,
66
59
IFormatProvider formatProvider = null ,
67
60
bool renderTemplate = true )
68
61
{
69
- var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy ( ) ;
70
- var ipAddress = IPAddress . Parse ( host ) ;
62
+ _writer = CreateSocketWriter ( hostAddress , port ) ;
63
+ _formatter = new SplunkJsonFormatter ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
64
+ }
65
+
66
+ /// <summary>
67
+ /// Creates an instance of the Splunk TCP sink.
68
+ /// </summary>
69
+ /// <param name="host">The Splunk Host</param>
70
+ /// <param name="port">The UDP port configured in Splunk</param>
71
+ /// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
72
+ public TcpSink (
73
+ string host ,
74
+ int port ,
75
+ ITextFormatter formatter )
76
+ {
77
+ _writer = CreateSocketWriter ( IPAddress . Parse ( host ) , port ) ;
78
+ _formatter = formatter ;
79
+ }
71
80
72
- _writer = new TcpSocketWriter ( ipAddress , port , reconnectionPolicy , 10000 ) ;
81
+ private static TcpSocketWriter CreateSocketWriter ( IPAddress hostAddress , int port )
82
+ {
83
+ var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy ( ) ;
73
84
74
- _jsonFormatter = new SplunkJsonFormatter ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
85
+ return new TcpSocketWriter ( hostAddress , port , reconnectionPolicy , 10000 ) ;
75
86
}
76
87
77
88
/// <inheritdoc/>
78
89
public void Emit ( LogEvent logEvent )
79
90
{
80
- var sw = new StringWriter ( ) ;
81
-
82
- _jsonFormatter . Format ( logEvent , sw ) ;
91
+ var sb = new StringBuilder ( ) ;
83
92
84
- var message = sw . ToString ( ) ;
93
+ using ( var sw = new StringWriter ( ) )
94
+ _formatter . Format ( logEvent , sw ) ;
85
95
86
- _writer . Enqueue ( message ) ;
96
+ _writer . Enqueue ( sb . ToString ( ) ) ;
87
97
}
88
98
89
99
/// <inheritdoc/>
90
100
public void Dispose ( )
91
101
{
92
- _writer . Dispose ( ) ;
102
+ _writer ? . Dispose ( ) ;
103
+ _writer = null ;
93
104
}
94
105
}
95
106
}
0 commit comments