1
- #if TCP
2
-
3
- // Copyright 2016 Serilog Contributors
1
+ // Copyright 2016 Serilog Contributors
4
2
//
5
3
// Licensed under the Apache License, Version 2.0 (the "License");
6
4
// you may not use this file except in compliance with the License.
14
12
// See the License for the specific language governing permissions and
15
13
// limitations under the License.
16
14
17
- using System ;
18
- using System . IO ;
19
- using System . Net ;
20
- using System . Text ;
21
15
using Serilog . Core ;
22
16
using Serilog . Events ;
23
17
using Serilog . Formatting ;
24
18
using Splunk . Logging ;
19
+ using System ;
20
+ using System . IO ;
21
+ using System . Text ;
25
22
26
23
namespace Serilog . Sinks . Splunk
27
24
{
@@ -30,7 +27,10 @@ namespace Serilog.Sinks.Splunk
30
27
/// </summary>
31
28
public class TcpSink : ILogEventSink , IDisposable
32
29
{
33
- readonly ITextFormatter _formatter ;
30
+ private readonly ITextFormatter _formatter ;
31
+ private readonly SplunkTcpSinkConnectionInfo _connectionInfo ;
32
+ private bool disposedValue = false ;
33
+
34
34
private TcpSocketWriter _writer ;
35
35
36
36
/// <summary>
@@ -39,70 +39,41 @@ public class TcpSink : ILogEventSink, IDisposable
39
39
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
40
40
/// <param name="formatProvider">Optional format provider</param>
41
41
/// <param name="renderTemplate">If true, the message template will be rendered</param>
42
- public TcpSink (
43
- SplunkTcpSinkConnectionInfo connectionInfo ,
44
- IFormatProvider formatProvider = null ,
45
- bool renderTemplate = true )
42
+ public TcpSink ( SplunkTcpSinkConnectionInfo connectionInfo , IFormatProvider formatProvider = null , bool renderTemplate = true )
43
+ : this ( connectionInfo , CreateDefaultFormatter ( formatProvider , renderTemplate ) )
46
44
{
47
- _writer = CreateSocketWriter ( connectionInfo ) ;
48
- _formatter = CreateDefaultFormatter ( formatProvider , renderTemplate ) ;
49
45
}
50
46
51
47
/// <summary>
52
48
/// Creates an instance of the Splunk TCP Sink.
53
49
/// </summary>
54
50
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
55
51
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
56
- public TcpSink (
57
- SplunkTcpSinkConnectionInfo connectionInfo ,
58
- ITextFormatter formatter )
52
+ public TcpSink ( SplunkTcpSinkConnectionInfo connectionInfo , ITextFormatter formatter )
59
53
{
60
- _writer = CreateSocketWriter ( connectionInfo ) ;
54
+ _connectionInfo = connectionInfo ;
61
55
_formatter = formatter ;
56
+ _writer = CreateSocketWriter ( connectionInfo ) ;
62
57
}
63
58
64
- /// <summary>
65
- /// Creates an instance of the Splunk TCP Sink
66
- /// </summary>
67
- /// <param name="host">The Splunk Host</param>
68
- /// <param name="port">The TCP port configured in Splunk</param>
69
- /// <param name="formatProvider">Optional format provider</param>
70
- /// <param name="renderTemplate">If true, the message template will be rendered</param>
71
- [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
72
- public TcpSink (
73
- string host ,
74
- int port ,
75
- IFormatProvider formatProvider = null ,
76
- bool renderTemplate = true ) : this ( new SplunkTcpSinkConnectionInfo ( host , port ) , formatProvider , renderTemplate )
77
- {
78
- }
79
-
80
- /// <summary>
81
- /// Creates an instance of the Splunk TCP Sink
82
- /// </summary>
83
- /// <param name="hostAddress">The Splunk Host</param>
84
- /// <param name="port">The TCP port configured in Splunk</param>
85
- /// <param name="formatProvider">Optional format provider</param>
86
- /// <param name="renderTemplate">If true, the message template will be rendered</param>
87
- [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
88
- public TcpSink (
89
- IPAddress hostAddress ,
90
- int port ,
91
- IFormatProvider formatProvider = null ,
92
- bool renderTemplate = true ) : this ( new SplunkTcpSinkConnectionInfo ( hostAddress , port ) , formatProvider , renderTemplate )
59
+ /// <inheritdoc/>
60
+ protected virtual void Dispose ( bool disposing )
93
61
{
62
+ if ( ! disposedValue )
63
+ {
64
+ if ( disposing )
65
+ {
66
+ _writer ? . Dispose ( ) ;
67
+ _writer = null ;
68
+ }
69
+ disposedValue = true ;
70
+ }
94
71
}
95
72
96
- private static TcpSocketWriter CreateSocketWriter ( SplunkTcpSinkConnectionInfo connectionInfo )
73
+ /// <inheritdoc/>
74
+ public void Dispose ( )
97
75
{
98
- var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy ( ) ;
99
-
100
- return new TcpSocketWriter ( connectionInfo . Host , connectionInfo . Port , reconnectionPolicy , connectionInfo . MaxQueueSize ) ;
101
- }
102
-
103
- private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
104
- {
105
- return new SplunkJsonFormatter ( renderTemplate , formatProvider ) ;
76
+ Dispose ( true ) ;
106
77
}
107
78
108
79
/// <inheritdoc/>
@@ -116,62 +87,16 @@ public void Emit(LogEvent logEvent)
116
87
_writer . Enqueue ( sb . ToString ( ) ) ;
117
88
}
118
89
119
- /// <inheritdoc/>
120
- public void Dispose ( )
90
+ private static TcpSocketWriter CreateSocketWriter ( SplunkTcpSinkConnectionInfo connectionInfo )
121
91
{
122
- _writer ? . Dispose ( ) ;
123
- _writer = null ;
124
- }
125
- }
126
-
127
-
128
- /// <summary>
129
- /// Defines connection info used to connect against Splunk
130
- /// using TCP.
131
- /// </summary>
132
- public class SplunkTcpSinkConnectionInfo
133
- {
134
- /// <summary>
135
- /// Default size of the socket writer queue.
136
- /// </summary>
137
- public const int DefaultMaxQueueSize = 10000 ;
138
-
139
- /// <summary>
140
- /// Splunk host.
141
- /// </summary>
142
- public IPAddress Host { get ; }
143
-
144
- /// <summary>
145
- /// Splunk port.
146
- /// </summary>
147
- public int Port { get ; }
148
-
149
- /// <summary>
150
- /// Max Queue size for the TCP socket writer.
151
- /// See <see cref="DefaultMaxQueueSize"/> for default value (10000).
152
- /// </summary>
153
- public int MaxQueueSize { get ; set ; } = DefaultMaxQueueSize ;
92
+ var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy ( ) ;
154
93
155
- /// <summary>
156
- /// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
157
- /// for defining connection info for connecting using TCP against Splunk.
158
- /// </summary>
159
- /// <param name="host">Splunk host.</param>
160
- /// <param name="port">Splunk TCP port.</param>
161
- public SplunkTcpSinkConnectionInfo ( string host , int port ) : this ( IPAddress . Parse ( host ) , port ) { }
94
+ return new TcpSocketWriter ( connectionInfo . Host , connectionInfo . Port , reconnectionPolicy , connectionInfo . MaxQueueSize ) ;
95
+ }
162
96
163
- /// <summary>
164
- /// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
165
- /// for defining connection info for connecting using TCP against Splunk.
166
- /// </summary>
167
- /// <param name="host">Splunk host.</param>
168
- /// <param name="port">Splunk TCP port.</param>
169
- public SplunkTcpSinkConnectionInfo ( IPAddress host , int port )
97
+ private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
170
98
{
171
- Host = host ;
172
- Port = port ;
99
+ return new SplunkJsonFormatter ( renderTemplate , formatProvider ) ;
173
100
}
174
101
}
175
102
}
176
-
177
- #endif
0 commit comments