|
24 | 24 | using NLog; |
25 | 25 | using MsgPack; |
26 | 26 | using MsgPack.Serialization; |
27 | | - |
| 27 | +using NLog.Common; |
| 28 | + |
28 | 29 | namespace NLog.Targets |
29 | 30 | { |
30 | 31 | internal class OrdinaryDictionarySerializer: MessagePackSerializer<IDictionary<string, object>> |
@@ -193,30 +194,47 @@ public class Fluentd : NLog.Targets.TargetWithLayout |
193 | 194 | protected override void InitializeTarget() |
194 | 195 | { |
195 | 196 | base.InitializeTarget(); |
| 197 | + } |
| 198 | + |
| 199 | + private void InitializeClient() |
| 200 | + { |
| 201 | + client = new TcpClient(); |
196 | 202 | client.NoDelay = this.NoDelay; |
197 | 203 | client.ReceiveBufferSize = this.ReceiveBufferSize; |
198 | 204 | client.SendBufferSize = this.SendBufferSize; |
199 | 205 | client.SendTimeout = this.SendTimeout; |
200 | 206 | client.ReceiveTimeout = this.ReceiveTimeout; |
201 | | - client.LingerState = new LingerOption(this.LingerEnabled, this.LingerTime); |
| 207 | + client.LingerState = new LingerOption(this.LingerEnabled, this.LingerTime); |
202 | 208 | } |
203 | 209 |
|
204 | 210 | protected void EnsureConnected() |
205 | 211 | { |
206 | 212 | try |
207 | 213 | { |
208 | | - if (!client.Connected) |
| 214 | + if(client == null) |
209 | 215 | { |
210 | | - client.Connect(this.Host, this.Port); |
211 | | - this.stream = this.client.GetStream(); |
212 | | - this.emitter = new FluentdEmitter(this.stream); |
| 216 | + InitializeClient(); |
| 217 | + ConnectClient(); |
| 218 | + } |
| 219 | + else if (!client.Connected) |
| 220 | + { |
| 221 | + Cleanup(); |
| 222 | + InitializeClient(); |
| 223 | + ConnectClient(); |
213 | 224 | } |
214 | 225 | } |
215 | 226 | catch (Exception e) |
216 | 227 | { |
217 | 228 | } |
218 | 229 | } |
219 | 230 |
|
| 231 | + private void ConnectClient() |
| 232 | + { |
| 233 | + client.Connect(this.Host, this.Port); |
| 234 | + this.stream = this.client.GetStream(); |
| 235 | + this.emitter = new FluentdEmitter(this.stream); |
| 236 | + } |
| 237 | + |
220 | 238 | protected void Cleanup() |
221 | 239 | { |
222 | 240 | if (this.stream != null) |
@@ -295,7 +313,6 @@ public Fluentd() |
295 | 313 | LingerTime = 1000; |
296 | 314 | EmitStackTraceWhenAvailable = false; |
297 | 315 | Tag = Assembly.GetCallingAssembly().GetName().Name; |
298 | | - client = new TcpClient(); |
299 | 316 | } |
300 | 317 | } |
301 | 318 | } |
0 commit comments