1
+ #if TCP
2
+
3
+ // Copyright 2016 Serilog Contributors
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+
17
+ using System ;
18
+ using System . IO ;
19
+ using System . Net ;
20
+ using System . Text ;
21
+ using Serilog . Core ;
22
+ using Serilog . Events ;
23
+ using Serilog . Formatting ;
24
+ using Splunk . Logging ;
25
+
26
+ namespace Serilog . Sinks . Splunk
27
+ {
28
+ /// <summary>
29
+ /// A sink that logs to Splunk over TCP
30
+ /// </summary>
31
+ public class TcpSink : ILogEventSink , IDisposable
32
+ {
33
+ readonly ITextFormatter _formatter ;
34
+ private TcpSocketWriter _writer ;
35
+
36
+ /// <summary>
37
+ /// Creates an instance of the Splunk TCP Sink.
38
+ /// </summary>
39
+ /// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
40
+ /// <param name="formatProvider">Optional format provider</param>
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 )
46
+ {
47
+ _writer = CreateSocketWriter ( connectionInfo ) ;
48
+ _formatter = CreateDefaultFormatter ( formatProvider , renderTemplate ) ;
49
+ }
50
+
51
+ /// <summary>
52
+ /// Creates an instance of the Splunk TCP Sink.
53
+ /// </summary>
54
+ /// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
55
+ /// <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 )
59
+ {
60
+ _writer = CreateSocketWriter ( connectionInfo ) ;
61
+ _formatter = formatter ;
62
+ }
63
+
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 )
93
+ {
94
+ }
95
+
96
+ private static TcpSocketWriter CreateSocketWriter ( SplunkTcpSinkConnectionInfo connectionInfo )
97
+ {
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 ( renderMessage : true , formatProvider : formatProvider , renderTemplate : renderTemplate ) ;
106
+ }
107
+
108
+ /// <inheritdoc/>
109
+ public void Emit ( LogEvent logEvent )
110
+ {
111
+ var sb = new StringBuilder ( ) ;
112
+
113
+ using ( var sw = new StringWriter ( sb ) )
114
+ _formatter . Format ( logEvent , sw ) ;
115
+
116
+ _writer . Enqueue ( sb . ToString ( ) ) ;
117
+ }
118
+
119
+ /// <inheritdoc/>
120
+ public void Dispose ( )
121
+ {
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 ;
154
+
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 ) { }
162
+
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 )
170
+ {
171
+ Host = host ;
172
+ Port = port ;
173
+ }
174
+ }
175
+ }
176
+
177
+ #endif
0 commit comments