Skip to content

Commit 0e407d2

Browse files
TcpSink accepts messageformatter
Also ensured disposing of writer and chained constuctor.
1 parent 712f8d0 commit 0e407d2

File tree

1 file changed

+35
-24
lines changed
  • src/Serilog.Sinks.Splunk.FullNetFx/Sinks/Splunk

1 file changed

+35
-24
lines changed

src/Serilog.Sinks.Splunk.FullNetFx/Sinks/Splunk/TcpSink.cs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
using System;
1616
using System.IO;
1717
using System.Net;
18-
using System.Net.Sockets;
1918
using System.Text;
2019
using Serilog.Core;
2120
using Serilog.Events;
22-
using Serilog.Formatting.Json;
21+
using Serilog.Formatting;
2322
using Splunk.Logging;
2423

2524
namespace Serilog.Sinks.Splunk
@@ -29,67 +28,79 @@ namespace Serilog.Sinks.Splunk
2928
/// </summary>
3029
public class TcpSink : ILogEventSink, IDisposable
3130
{
32-
readonly JsonFormatter _jsonFormatter;
31+
readonly ITextFormatter _formatter;
3332
private TcpSocketWriter _writer;
3433

3534
/// <summary>
3635
/// Creates an instance of the Splunk TCP Sink
3736
/// </summary>
38-
/// <param name="hostAddress">The Splunk Host</param>
37+
/// <param name="host">The Splunk Host</param>
3938
/// <param name="port">The UDP port configured in Splunk</param>
4039
/// <param name="formatProvider">Optional format provider</param>
4140
/// <param name="renderTemplate">If true, the message template will be rendered</param>
42-
4341
public TcpSink(
44-
IPAddress hostAddress,
42+
string host,
4543
int port,
4644
IFormatProvider formatProvider = null,
47-
bool renderTemplate = true)
45+
bool renderTemplate = true) : this(IPAddress.Parse(host), port, formatProvider, renderTemplate)
4846
{
49-
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
50-
51-
_writer = new TcpSocketWriter(hostAddress, port, reconnectionPolicy, 10000);
52-
53-
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
5447
}
5548

5649
/// <summary>
5750
/// Creates an instance of the Splunk TCP Sink
5851
/// </summary>
59-
/// <param name="host">The Splunk Host</param>
52+
/// <param name="hostAddress">The Splunk Host</param>
6053
/// <param name="port">The UDP port configured in Splunk</param>
6154
/// <param name="formatProvider">Optional format provider</param>
6255
/// <param name="renderTemplate">If true, the message template will be rendered</param>
6356
public TcpSink(
64-
string host,
57+
IPAddress hostAddress,
6558
int port,
6659
IFormatProvider formatProvider = null,
6760
bool renderTemplate = true)
6861
{
69-
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
70-
var ipAddress = IPAddress.Parse(host);
62+
_writer = CreateSocketWriter(hostAddress, port);
63+
_formatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
64+
}
65+
66+
/// <summary>
67+
/// Creates an instance of the Splunk TCP sink.
68+
/// </summary>
69+
/// <param name="host">The Splunk Host</param>
70+
/// <param name="port">The UDP port configured in Splunk</param>
71+
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
72+
public TcpSink(
73+
string host,
74+
int port,
75+
ITextFormatter formatter)
76+
{
77+
_writer = CreateSocketWriter(IPAddress.Parse(host), port);
78+
_formatter = formatter;
79+
}
7180

72-
_writer = new TcpSocketWriter(ipAddress, port, reconnectionPolicy, 10000);
81+
private static TcpSocketWriter CreateSocketWriter(IPAddress hostAddress, int port)
82+
{
83+
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
7384

74-
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
85+
return new TcpSocketWriter(hostAddress, port, reconnectionPolicy, 10000);
7586
}
7687

7788
/// <inheritdoc/>
7889
public void Emit(LogEvent logEvent)
7990
{
80-
var sw = new StringWriter();
81-
82-
_jsonFormatter.Format(logEvent, sw);
91+
var sb = new StringBuilder();
8392

84-
var message = sw.ToString();
93+
using (var sw = new StringWriter())
94+
_formatter.Format(logEvent, sw);
8595

86-
_writer.Enqueue(message);
96+
_writer.Enqueue(sb.ToString());
8797
}
8898

8999
/// <inheritdoc/>
90100
public void Dispose()
91101
{
92-
_writer.Dispose();
102+
_writer?.Dispose();
103+
_writer = null;
93104
}
94105
}
95106
}

0 commit comments

Comments
 (0)