Skip to content

Commit d8007c8

Browse files
authored
Merge pull request #6 from dorzeidman/master
Fixed TCP-Client re-connection after socket disconnects
2 parents 180c407 + de74354 commit d8007c8

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/NLog.Targets.Fluentd/Fluentd.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
using NLog;
2525
using MsgPack;
2626
using MsgPack.Serialization;
27-
27+
using NLog.Common;
28+
2829
namespace NLog.Targets
2930
{
3031
internal class OrdinaryDictionarySerializer: MessagePackSerializer<IDictionary<string, object>>
@@ -193,30 +194,47 @@ public class Fluentd : NLog.Targets.TargetWithLayout
193194
protected override void InitializeTarget()
194195
{
195196
base.InitializeTarget();
197+
}
198+
199+
private void InitializeClient()
200+
{
201+
client = new TcpClient();
196202
client.NoDelay = this.NoDelay;
197203
client.ReceiveBufferSize = this.ReceiveBufferSize;
198204
client.SendBufferSize = this.SendBufferSize;
199205
client.SendTimeout = this.SendTimeout;
200206
client.ReceiveTimeout = this.ReceiveTimeout;
201-
client.LingerState = new LingerOption(this.LingerEnabled, this.LingerTime);
207+
client.LingerState = new LingerOption(this.LingerEnabled, this.LingerTime);
202208
}
203209

204210
protected void EnsureConnected()
205211
{
206212
try
207213
{
208-
if (!client.Connected)
214+
if(client == null)
209215
{
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();
213224
}
214225
}
215226
catch (Exception e)
216227
{
217228
}
218229
}
219230

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+
220238
protected void Cleanup()
221239
{
222240
if (this.stream != null)
@@ -295,7 +313,6 @@ public Fluentd()
295313
LingerTime = 1000;
296314
EmitStackTraceWhenAvailable = false;
297315
Tag = Assembly.GetCallingAssembly().GetName().Name;
298-
client = new TcpClient();
299316
}
300317
}
301318
}

0 commit comments

Comments
 (0)