Skip to content

Commit c895d83

Browse files
SplunkUdpSinkConnectionInfo and injectable formatter
Supports: - Host - Port Also ensured disposal of writer in emit.
1 parent 8fdb05c commit c895d83

File tree

4 files changed

+140
-24
lines changed

4 files changed

+140
-24
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,46 @@ namespace Serilog
2727
/// </summary>
2828
public static class LoggerConfigurationSplunkExtensions
2929
{
30+
/// <summary>
31+
/// Adds a sink that writes log events as to a Splunk instance via UDP.
32+
/// </summary>
33+
/// <param name="loggerConfiguration">The logger config</param>
34+
/// <param name="connectionInfo"></param>
35+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
36+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
37+
/// <param name="renderTemplate">If true, the message template is rendered</param>
38+
/// <returns></returns>
39+
public static LoggerConfiguration SplunkViaUdp(
40+
this LoggerSinkConfiguration loggerConfiguration,
41+
SplunkUdpSinkConnectionInfo connectionInfo,
42+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
43+
IFormatProvider formatProvider = null,
44+
bool renderTemplate = true)
45+
{
46+
var sink = new UdpSink(connectionInfo, formatProvider, renderTemplate);
47+
48+
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
49+
}
50+
51+
/// <summary>
52+
/// Adds a sink that writes log events as to a Splunk instance via UDP.
53+
/// </summary>
54+
/// <param name="loggerConfiguration">The logger config</param>
55+
/// <param name="connectionInfo"></param>
56+
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
57+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
58+
/// <returns></returns>
59+
public static LoggerConfiguration SplunkViaUdp(
60+
this LoggerSinkConfiguration loggerConfiguration,
61+
SplunkUdpSinkConnectionInfo connectionInfo,
62+
ITextFormatter formatter,
63+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
64+
{
65+
var sink = new UdpSink(connectionInfo, formatter);
66+
67+
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
68+
}
69+
3070
/// <summary>
3171
/// Adds a sink that writes log events as to a Splunk instance via UDP.
3272
/// </summary>
@@ -38,6 +78,7 @@ public static class LoggerConfigurationSplunkExtensions
3878
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
3979
/// <returns></returns>
4080
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
81+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
4182
public static LoggerConfiguration SplunkViaUdp(
4283
this LoggerSinkConfiguration loggerConfiguration,
4384
string host,
@@ -63,6 +104,7 @@ public static LoggerConfiguration SplunkViaUdp(
63104
/// <param name="renderTemplate">If ture, the message template is rendered</param>
64105
/// <returns>The logger configuration</returns>
65106
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
107+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
66108
public static LoggerConfiguration SplunkViaUdp(
67109
this LoggerSinkConfiguration loggerConfiguration,
68110
IPAddress hostAddresss,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</Compile>
8787
<Compile Include="LoggerConfigurationSplunkExtensions.cs" />
8888
<Compile Include="Sinks\Splunk\SplunkTcpSinkConnectionInfo.cs" />
89+
<Compile Include="Sinks\Splunk\SplunkUdpSinkConnectionInfo.cs" />
8990
<Compile Include="Sinks\Splunk\TcpSink.cs" />
9091
<Compile Include="Sinks\Splunk\UdpSink.cs" />
9192
</ItemGroup>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Net;
2+
3+
namespace Serilog.Sinks.Splunk
4+
{
5+
/// <summary>
6+
/// Defines connection info used to connect against Splunk
7+
/// using UDP.
8+
/// </summary>
9+
public class SplunkUdpSinkConnectionInfo
10+
{
11+
/// <summary>
12+
/// Splunk host.
13+
/// </summary>
14+
public IPAddress Host { get; }
15+
16+
/// <summary>
17+
/// Splunk port.
18+
/// </summary>
19+
public int Port { get; }
20+
21+
/// <summary>
22+
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
23+
/// for defining connection info for connecting using UDP against Splunk.
24+
/// </summary>
25+
/// <param name="host">Splunk host.</param>
26+
/// <param name="port">Splunk UDP port.</param>
27+
public SplunkUdpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port){ }
28+
29+
/// <summary>
30+
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
31+
/// for defining connection info for connecting using UDP against Splunk.
32+
/// </summary>
33+
/// <param name="host">Splunk host.</param>
34+
/// <param name="port">Splunk UDP port.</param>
35+
public SplunkUdpSinkConnectionInfo(IPAddress host, int port)
36+
{
37+
Host = host;
38+
Port = port;
39+
}
40+
}
41+
}

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

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using System.Text;
2020
using Serilog.Core;
2121
using Serilog.Events;
22-
using Serilog.Formatting.Json;
22+
using Serilog.Formatting;
2323

2424
namespace Serilog.Sinks.Splunk
2525
{
@@ -28,22 +28,41 @@ namespace Serilog.Sinks.Splunk
2828
/// </summary>
2929
public class UdpSink : ILogEventSink, IDisposable
3030
{
31-
readonly Socket _socket;
32-
readonly JsonFormatter _jsonFormatter;
31+
Socket _socket;
32+
readonly ITextFormatter _formatter;
3333

3434
/// <summary>
35-
/// Creates an instance of the Splunk UDP Sink
35+
/// Creates an instance of the Splunk TCP Sink.
3636
/// </summary>
37-
/// <param name="hostAddress">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>
41-
public UdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
40+
public UdpSink(
41+
SplunkUdpSinkConnectionInfo connectionInfo,
42+
IFormatProvider formatProvider = null,
43+
bool renderTemplate = true)
4244
{
43-
_socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
44-
_socket.Connect(hostAddress, port);
45+
Connect(connectionInfo);
46+
_formatter = CreateDefaultFormatter(formatProvider, renderTemplate);
47+
}
4548

46-
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
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 UdpSink(
55+
SplunkUdpSinkConnectionInfo connectionInfo,
56+
ITextFormatter formatter)
57+
{
58+
Connect(connectionInfo);
59+
_formatter = formatter;
60+
}
61+
62+
private void Connect(SplunkUdpSinkConnectionInfo connectionInfo)
63+
{
64+
_socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
65+
_socket.Connect(connectionInfo.Host, connectionInfo.Port);
4766
}
4867

4968
/// <summary>
@@ -53,34 +72,47 @@ public UdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider =
5372
/// <param name="port">The UDP port configured in Splunk</param>
5473
/// <param name="formatProvider">Optional format provider</param>
5574
/// <param name="renderTemplate">If true, the message template is rendered</param>
56-
public UdpSink(string host, int port, IFormatProvider formatProvider = null,
57-
bool renderTemplate = true)
75+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
76+
public UdpSink(string host, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
77+
: this(new SplunkUdpSinkConnectionInfo(host, port), formatProvider, renderTemplate)
5878
{
59-
_socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
60-
_socket.Connect(host, port);
79+
}
6180

62-
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate:renderTemplate);
81+
/// <summary>
82+
/// Creates an instance of the Splunk UDP Sink
83+
/// </summary>
84+
/// <param name="hostAddress">The Splunk Host</param>
85+
/// <param name="port">The UDP port configured in Splunk</param>
86+
/// <param name="formatProvider">Optional format provider</param>
87+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
88+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
89+
public UdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
90+
: this(new SplunkUdpSinkConnectionInfo(hostAddress, port),formatProvider, renderTemplate)
91+
{
92+
}
93+
94+
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
95+
{
96+
return new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
6397
}
6498

6599
/// <inheritdoc/>
66100
public void Emit(LogEvent logEvent)
67101
{
68-
var sw = new StringWriter();
102+
var sb = new StringBuilder();
69103

70-
_jsonFormatter.Format(logEvent, sw);
104+
using (var sw = new StringWriter(sb))
105+
_formatter.Format(logEvent, sw);
71106

72-
var message = sw.ToString();
73-
74-
_socket.Send(Encoding.UTF8.GetBytes(message));
107+
_socket.Send(Encoding.UTF8.GetBytes(sb.ToString()));
75108
}
76109

77110
/// <inheritdoc/>
78111
public void Dispose()
79112
{
80-
_socket.Close();
81-
_socket.Dispose();
113+
_socket?.Close();
114+
_socket?.Dispose();
115+
_socket = null;
82116
}
83117
}
84118
}
85-
86-

0 commit comments

Comments
 (0)