1
- #if UDP
2
-
3
-
4
- // Copyright 2016 Serilog Contributors
1
+ // Copyright 2016 Serilog Contributors
5
2
//
6
3
// Licensed under the Apache License, Version 2.0 (the "License");
7
4
// you may not use this file except in compliance with the License.
15
12
// See the License for the specific language governing permissions and
16
13
// limitations under the License.
17
14
15
+ using Serilog . Core ;
16
+ using Serilog . Events ;
17
+ using Serilog . Formatting ;
18
18
using System ;
19
19
using System . IO ;
20
- using System . Net ;
21
20
using System . Net . Sockets ;
22
21
using System . Text ;
23
- using Serilog . Core ;
24
- using Serilog . Events ;
25
- using Serilog . Formatting ;
26
22
27
23
namespace Serilog . Sinks . Splunk
28
24
{
29
25
/// <summary>
30
26
/// A sink that logs to Splunk over UDP
31
- /// </summary>
27
+ /// </summary>r
32
28
public class UdpSink : ILogEventSink , IDisposable
33
29
{
34
- Socket _socket ;
35
- readonly ITextFormatter _formatter ;
30
+ private Socket _socket ;
31
+ private readonly ITextFormatter _formatter ;
32
+ private bool disposedValue = false ;
36
33
37
34
/// <summary>
38
35
/// Creates an instance of the Splunk UDP Sink.
@@ -68,32 +65,6 @@ private void Connect(SplunkUdpSinkConnectionInfo connectionInfo)
68
65
_socket . Connect ( connectionInfo . Host , connectionInfo . Port ) ;
69
66
}
70
67
71
- /// <summary>
72
- /// Creates an instance of the Splunk UDP Sink
73
- /// </summary>
74
- /// <param name="host">The Splunk Host</param>
75
- /// <param name="port">The UDP port configured in Splunk</param>
76
- /// <param name="formatProvider">Optional format provider</param>
77
- /// <param name="renderTemplate">If true, the message template is rendered</param>
78
- [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
79
- public UdpSink ( string host , int port , IFormatProvider formatProvider = null , bool renderTemplate = true )
80
- : this ( new SplunkUdpSinkConnectionInfo ( host , port ) , formatProvider , renderTemplate )
81
- {
82
- }
83
-
84
- /// <summary>
85
- /// Creates an instance of the Splunk UDP Sink
86
- /// </summary>
87
- /// <param name="hostAddress">The Splunk Host</param>
88
- /// <param name="port">The UDP port configured in Splunk</param>
89
- /// <param name="formatProvider">Optional format provider</param>
90
- /// <param name="renderTemplate">If true, the message template will be rendered</param>
91
- [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
92
- public UdpSink ( IPAddress hostAddress , int port , IFormatProvider formatProvider = null , bool renderTemplate = true )
93
- : this ( new SplunkUdpSinkConnectionInfo ( hostAddress , port ) , formatProvider , renderTemplate )
94
- {
95
- }
96
-
97
68
private static SplunkJsonFormatter CreateDefaultFormatter ( IFormatProvider formatProvider , bool renderTemplate )
98
69
{
99
70
return new SplunkJsonFormatter ( renderTemplate , formatProvider ) ;
@@ -111,50 +82,26 @@ public void Emit(LogEvent logEvent)
111
82
}
112
83
113
84
/// <inheritdoc/>
114
- public void Dispose ( )
85
+ protected virtual void Dispose ( bool disposing )
115
86
{
116
- _socket ? . Close ( ) ;
117
- _socket ? . Dispose ( ) ;
118
- _socket = null ;
87
+ if ( ! disposedValue )
88
+ {
89
+ if ( disposing )
90
+ {
91
+ _socket ? . Close ( ) ;
92
+ _socket ? . Dispose ( ) ;
93
+ _socket = null ;
94
+ }
95
+
96
+ disposedValue = true ;
97
+ }
119
98
}
120
- }
121
-
122
- /// <summary>
123
- /// Defines connection info used to connect against Splunk
124
- /// using UDP.
125
- /// </summary>
126
- public class SplunkUdpSinkConnectionInfo
127
- {
128
- /// <summary>
129
- /// Splunk host.
130
- /// </summary>
131
- public IPAddress Host { get ; }
132
99
133
- /// <summary>
134
- /// Splunk port.
135
- /// </summary>
136
- public int Port { get ; }
137
-
138
- /// <summary>
139
- /// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
140
- /// for defining connection info for connecting using UDP against Splunk.
141
- /// </summary>
142
- /// <param name="host">Splunk host.</param>
143
- /// <param name="port">Splunk UDP port.</param>
144
- public SplunkUdpSinkConnectionInfo ( string host , int port ) : this ( IPAddress . Parse ( host ) , port ) { }
145
-
146
- /// <summary>
147
- /// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
148
- /// for defining connection info for connecting using UDP against Splunk.
149
- /// </summary>
150
- /// <param name="host">Splunk host.</param>
151
- /// <param name="port">Splunk UDP port.</param>
152
- public SplunkUdpSinkConnectionInfo ( IPAddress host , int port )
100
+ /// <inheritdoc/>
101
+ public void Dispose ( )
153
102
{
154
- Host = host ;
155
- Port = port ;
103
+ Dispose ( true ) ;
156
104
}
157
105
}
158
106
}
159
107
160
- #endif
0 commit comments