Skip to content

Commit e7ddc65

Browse files
SplunkTcpSinkConnectionInfo for collecting TCP config info
Supports: - Host - Port - Optinally queue size Marked other overloads as obsolete so that they can be removed in the future.
1 parent 0e407d2 commit e7ddc65

File tree

4 files changed

+137
-25
lines changed

4 files changed

+137
-25
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Net;
1717
using Serilog.Configuration;
1818
using Serilog.Events;
19+
using Serilog.Formatting;
1920
using Serilog.Formatting.Display;
2021
using Serilog.Sinks.Splunk;
2122

@@ -75,6 +76,46 @@ public static LoggerConfiguration SplunkViaUdp(
7576
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
7677
}
7778

79+
/// <summary>
80+
/// Adds a sink that writes log events as to a Splunk instance via TCP.
81+
/// </summary>
82+
/// <param name="loggerConfiguration">The logger config</param>
83+
/// <param name="connectionInfo"></param>
84+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
85+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
86+
/// <param name="renderTemplate">If true, the message template is rendered</param>
87+
/// <returns></returns>
88+
public static LoggerConfiguration SplunkViaTcp(
89+
this LoggerSinkConfiguration loggerConfiguration,
90+
SplunkTcpSinkConnectionInfo connectionInfo,
91+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
92+
IFormatProvider formatProvider = null,
93+
bool renderTemplate = true)
94+
{
95+
var sink = new TcpSink(connectionInfo, formatProvider, renderTemplate);
96+
97+
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
98+
}
99+
100+
/// <summary>
101+
/// Adds a sink that writes log events as to a Splunk instance via TCP.
102+
/// </summary>
103+
/// <param name="loggerConfiguration">The logger config</param>
104+
/// <param name="connectionInfo"></param>
105+
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
106+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
107+
/// <returns></returns>
108+
public static LoggerConfiguration SplunkViaTcp(
109+
this LoggerSinkConfiguration loggerConfiguration,
110+
SplunkTcpSinkConnectionInfo connectionInfo,
111+
ITextFormatter formatter,
112+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
113+
{
114+
var sink = new TcpSink(connectionInfo, formatter);
115+
116+
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
117+
}
118+
78119
/// <summary>
79120
/// Adds a sink that writes log events as to a Splunk instance via TCP.
80121
/// </summary>
@@ -86,6 +127,7 @@ public static LoggerConfiguration SplunkViaUdp(
86127
/// <param name="renderTemplate">If true, the message template is rendered</param>
87128
/// <returns></returns>
88129
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
130+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
89131
public static LoggerConfiguration SplunkViaTcp(
90132
this LoggerSinkConfiguration loggerConfiguration,
91133
IPAddress hostAddresss,
@@ -110,6 +152,7 @@ public static LoggerConfiguration SplunkViaTcp(
110152
/// <param name="renderTemplate">If ture, the message template is rendered</param>
111153
/// <returns></returns>
112154
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
155+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
113156
public static LoggerConfiguration SplunkViaTcp(
114157
this LoggerSinkConfiguration loggerConfiguration,
115158
string host,
@@ -122,6 +165,5 @@ public static LoggerConfiguration SplunkViaTcp(
122165

123166
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
124167
}
125-
126168
}
127169
}

src/Serilog.Sinks.Splunk.FullNetFx/Serilog.Sinks.Splunk.FullNetFx.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Link>Properties\CommonAssemblyInfo.cs</Link>
8686
</Compile>
8787
<Compile Include="LoggerConfigurationSplunkExtensions.cs" />
88+
<Compile Include="Sinks\Splunk\SplunkTcpSinkConnectionInfo.cs" />
8889
<Compile Include="Sinks\Splunk\TcpSink.cs" />
8990
<Compile Include="Sinks\Splunk\UdpSink.cs" />
9091
</ItemGroup>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Net;
2+
3+
namespace Serilog.Sinks.Splunk
4+
{
5+
/// <summary>
6+
/// Defines connection info used to connect against Splunk
7+
/// using TCP.
8+
/// </summary>
9+
public class SplunkTcpSinkConnectionInfo
10+
{
11+
/// <summary>
12+
/// Default size of the socket writer queue.
13+
/// </summary>
14+
public const int DefaultMaxQueueSize = 10000;
15+
16+
/// <summary>
17+
/// Splunk host.
18+
/// </summary>
19+
public IPAddress Host { get; }
20+
21+
/// <summary>
22+
/// Splunk port.
23+
/// </summary>
24+
public int Port { get; }
25+
26+
/// <summary>
27+
/// Max Queue size for the TCP socket writer.
28+
/// See <see cref="DefaultMaxQueueSize"/> for default value (10000).
29+
/// </summary>
30+
public int MaxQueueSize { get; set; } = DefaultMaxQueueSize;
31+
32+
/// <summary>
33+
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
34+
/// for defining connection info for connecting using TCP against Splunk.
35+
/// </summary>
36+
/// <param name="host">Splunk host.</param>
37+
/// <param name="port">Splunk TCP port.</param>
38+
public SplunkTcpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port){ }
39+
40+
/// <summary>
41+
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
42+
/// for defining connection info for connecting using TCP against Splunk.
43+
/// </summary>
44+
/// <param name="host">Splunk host.</param>
45+
/// <param name="port">Splunk TCP port.</param>
46+
public SplunkTcpSinkConnectionInfo(IPAddress host, int port)
47+
{
48+
Host = host;
49+
Port = port;
50+
}
51+
}
52+
}

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

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,75 @@ public class TcpSink : ILogEventSink, IDisposable
3232
private TcpSocketWriter _writer;
3333

3434
/// <summary>
35-
/// Creates an instance of the Splunk TCP Sink
35+
/// Creates an instance of the Splunk TCP Sink.
3636
/// </summary>
37-
/// <param name="host">The Splunk Host</param>
38-
/// <param name="port">The UDP port configured in Splunk</param>
37+
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
3938
/// <param name="formatProvider">Optional format provider</param>
4039
/// <param name="renderTemplate">If true, the message template will be rendered</param>
4140
public TcpSink(
42-
string host,
43-
int port,
41+
SplunkTcpSinkConnectionInfo connectionInfo,
4442
IFormatProvider formatProvider = null,
45-
bool renderTemplate = true) : this(IPAddress.Parse(host), port, formatProvider, renderTemplate)
43+
bool renderTemplate = true)
4644
{
45+
_writer = CreateSocketWriter(connectionInfo);
46+
_formatter = CreateDefaultFormatter(formatProvider, renderTemplate);
47+
}
48+
49+
/// <summary>
50+
/// Creates an instance of the Splunk TCP Sink.
51+
/// </summary>
52+
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
53+
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
54+
public TcpSink(
55+
SplunkTcpSinkConnectionInfo connectionInfo,
56+
ITextFormatter formatter)
57+
{
58+
_writer = CreateSocketWriter(connectionInfo);
59+
_formatter = formatter;
4760
}
4861

4962
/// <summary>
5063
/// Creates an instance of the Splunk TCP Sink
5164
/// </summary>
52-
/// <param name="hostAddress">The Splunk Host</param>
53-
/// <param name="port">The UDP port configured in Splunk</param>
65+
/// <param name="host">The Splunk Host</param>
66+
/// <param name="port">The TCP port configured in Splunk</param>
5467
/// <param name="formatProvider">Optional format provider</param>
5568
/// <param name="renderTemplate">If true, the message template will be rendered</param>
69+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
5670
public TcpSink(
57-
IPAddress hostAddress,
71+
string host,
5872
int port,
5973
IFormatProvider formatProvider = null,
60-
bool renderTemplate = true)
74+
bool renderTemplate = true) : this(new SplunkTcpSinkConnectionInfo(host, port), formatProvider, renderTemplate)
6175
{
62-
_writer = CreateSocketWriter(hostAddress, port);
63-
_formatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
6476
}
6577

6678
/// <summary>
67-
/// Creates an instance of the Splunk TCP sink.
79+
/// Creates an instance of the Splunk TCP Sink
6880
/// </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>
81+
/// <param name="hostAddress">The Splunk Host</param>
82+
/// <param name="port">The TCP port configured in Splunk</param>
83+
/// <param name="formatProvider">Optional format provider</param>
84+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
85+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
7286
public TcpSink(
73-
string host,
87+
IPAddress hostAddress,
7488
int port,
75-
ITextFormatter formatter)
89+
IFormatProvider formatProvider = null,
90+
bool renderTemplate = true) : this(new SplunkTcpSinkConnectionInfo(hostAddress, port), formatProvider, renderTemplate)
7691
{
77-
_writer = CreateSocketWriter(IPAddress.Parse(host), port);
78-
_formatter = formatter;
7992
}
8093

81-
private static TcpSocketWriter CreateSocketWriter(IPAddress hostAddress, int port)
94+
private static TcpSocketWriter CreateSocketWriter(SplunkTcpSinkConnectionInfo connectionInfo)
8295
{
8396
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
8497

85-
return new TcpSocketWriter(hostAddress, port, reconnectionPolicy, 10000);
98+
return new TcpSocketWriter(connectionInfo.Host, connectionInfo.Port, reconnectionPolicy, connectionInfo.MaxQueueSize);
99+
}
100+
101+
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
102+
{
103+
return new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
86104
}
87105

88106
/// <inheritdoc/>
@@ -103,5 +121,4 @@ public void Dispose()
103121
_writer = null;
104122
}
105123
}
106-
}
107-
124+
}

0 commit comments

Comments
 (0)