Skip to content

Commit 3629606

Browse files
committed
Added legacy sinks
1 parent f1a0047 commit 3629606

File tree

8 files changed

+759
-5
lines changed

8 files changed

+759
-5
lines changed

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// Copyright 2014 Serilog Contributors
2+
// Copyright 2016 Serilog Contributors
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.

src/Serilog.Sinks.Splunk/Sinks/Splunk/RepeatAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 Serilog Contributors
1+
// Copyright 2016 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#if TCP
2+
3+
// Copyright 2016 Serilog Contributors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
using System;
18+
using System.IO;
19+
using System.Net;
20+
using System.Text;
21+
using Serilog.Core;
22+
using Serilog.Events;
23+
using Serilog.Formatting;
24+
using Splunk.Logging;
25+
26+
namespace Serilog.Sinks.Splunk
27+
{
28+
/// <summary>
29+
/// A sink that logs to Splunk over TCP
30+
/// </summary>
31+
public class TcpSink : ILogEventSink, IDisposable
32+
{
33+
readonly ITextFormatter _formatter;
34+
private TcpSocketWriter _writer;
35+
36+
/// <summary>
37+
/// Creates an instance of the Splunk TCP Sink.
38+
/// </summary>
39+
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
40+
/// <param name="formatProvider">Optional format provider</param>
41+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
42+
public TcpSink(
43+
SplunkTcpSinkConnectionInfo connectionInfo,
44+
IFormatProvider formatProvider = null,
45+
bool renderTemplate = true)
46+
{
47+
_writer = CreateSocketWriter(connectionInfo);
48+
_formatter = CreateDefaultFormatter(formatProvider, renderTemplate);
49+
}
50+
51+
/// <summary>
52+
/// Creates an instance of the Splunk TCP Sink.
53+
/// </summary>
54+
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
55+
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
56+
public TcpSink(
57+
SplunkTcpSinkConnectionInfo connectionInfo,
58+
ITextFormatter formatter)
59+
{
60+
_writer = CreateSocketWriter(connectionInfo);
61+
_formatter = formatter;
62+
}
63+
64+
/// <summary>
65+
/// Creates an instance of the Splunk TCP Sink
66+
/// </summary>
67+
/// <param name="host">The Splunk Host</param>
68+
/// <param name="port">The TCP port configured in Splunk</param>
69+
/// <param name="formatProvider">Optional format provider</param>
70+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
71+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
72+
public TcpSink(
73+
string host,
74+
int port,
75+
IFormatProvider formatProvider = null,
76+
bool renderTemplate = true) : this(new SplunkTcpSinkConnectionInfo(host, port), formatProvider, renderTemplate)
77+
{
78+
}
79+
80+
/// <summary>
81+
/// Creates an instance of the Splunk TCP Sink
82+
/// </summary>
83+
/// <param name="hostAddress">The Splunk Host</param>
84+
/// <param name="port">The TCP port configured in Splunk</param>
85+
/// <param name="formatProvider">Optional format provider</param>
86+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
87+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
88+
public TcpSink(
89+
IPAddress hostAddress,
90+
int port,
91+
IFormatProvider formatProvider = null,
92+
bool renderTemplate = true) : this(new SplunkTcpSinkConnectionInfo(hostAddress, port), formatProvider, renderTemplate)
93+
{
94+
}
95+
96+
private static TcpSocketWriter CreateSocketWriter(SplunkTcpSinkConnectionInfo connectionInfo)
97+
{
98+
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
99+
100+
return new TcpSocketWriter(connectionInfo.Host, connectionInfo.Port, reconnectionPolicy, connectionInfo.MaxQueueSize);
101+
}
102+
103+
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
104+
{
105+
return new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
106+
}
107+
108+
/// <inheritdoc/>
109+
public void Emit(LogEvent logEvent)
110+
{
111+
var sb = new StringBuilder();
112+
113+
using (var sw = new StringWriter(sb))
114+
_formatter.Format(logEvent, sw);
115+
116+
_writer.Enqueue(sb.ToString());
117+
}
118+
119+
/// <inheritdoc/>
120+
public void Dispose()
121+
{
122+
_writer?.Dispose();
123+
_writer = null;
124+
}
125+
}
126+
127+
128+
/// <summary>
129+
/// Defines connection info used to connect against Splunk
130+
/// using TCP.
131+
/// </summary>
132+
public class SplunkTcpSinkConnectionInfo
133+
{
134+
/// <summary>
135+
/// Default size of the socket writer queue.
136+
/// </summary>
137+
public const int DefaultMaxQueueSize = 10000;
138+
139+
/// <summary>
140+
/// Splunk host.
141+
/// </summary>
142+
public IPAddress Host { get; }
143+
144+
/// <summary>
145+
/// Splunk port.
146+
/// </summary>
147+
public int Port { get; }
148+
149+
/// <summary>
150+
/// Max Queue size for the TCP socket writer.
151+
/// See <see cref="DefaultMaxQueueSize"/> for default value (10000).
152+
/// </summary>
153+
public int MaxQueueSize { get; set; } = DefaultMaxQueueSize;
154+
155+
/// <summary>
156+
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
157+
/// for defining connection info for connecting using TCP against Splunk.
158+
/// </summary>
159+
/// <param name="host">Splunk host.</param>
160+
/// <param name="port">Splunk TCP port.</param>
161+
public SplunkTcpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port) { }
162+
163+
/// <summary>
164+
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
165+
/// for defining connection info for connecting using TCP against Splunk.
166+
/// </summary>
167+
/// <param name="host">Splunk host.</param>
168+
/// <param name="port">Splunk TCP port.</param>
169+
public SplunkTcpSinkConnectionInfo(IPAddress host, int port)
170+
{
171+
Host = host;
172+
Port = port;
173+
}
174+
}
175+
}
176+
177+
#endif
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#if UDP
2+
3+
4+
// Copyright 2016 Serilog Contributors
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
using System;
19+
using System.IO;
20+
using System.Net;
21+
using System.Net.Sockets;
22+
using System.Text;
23+
using Serilog.Core;
24+
using Serilog.Events;
25+
using Serilog.Formatting;
26+
27+
namespace Serilog.Sinks.Splunk
28+
{
29+
/// <summary>
30+
/// A sink that logs to Splunk over Udp
31+
/// </summary>
32+
public class UdpSink : ILogEventSink, IDisposable
33+
{
34+
Socket _socket;
35+
readonly ITextFormatter _formatter;
36+
37+
/// <summary>
38+
/// Creates an instance of the Splunk TCP Sink.
39+
/// </summary>
40+
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
41+
/// <param name="formatProvider">Optional format provider</param>
42+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
43+
public UdpSink(
44+
SplunkUdpSinkConnectionInfo connectionInfo,
45+
IFormatProvider formatProvider = null,
46+
bool renderTemplate = true)
47+
{
48+
Connect(connectionInfo);
49+
_formatter = CreateDefaultFormatter(formatProvider, renderTemplate);
50+
}
51+
52+
/// <summary>
53+
/// Creates an instance of the Splunk TCP Sink.
54+
/// </summary>
55+
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
56+
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
57+
public UdpSink(
58+
SplunkUdpSinkConnectionInfo connectionInfo,
59+
ITextFormatter formatter)
60+
{
61+
Connect(connectionInfo);
62+
_formatter = formatter;
63+
}
64+
65+
private void Connect(SplunkUdpSinkConnectionInfo connectionInfo)
66+
{
67+
_socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
68+
_socket.Connect(connectionInfo.Host, connectionInfo.Port);
69+
}
70+
71+
/// <summary>
72+
/// Creates an instance of the Splunk UDP Sink
73+
/// </summary>
74+
/// <param name="host">The Splunk Host</param>
75+
/// <param name="port">The UDP port configured in Splunk</param>
76+
/// <param name="formatProvider">Optional format provider</param>
77+
/// <param name="renderTemplate">If true, the message template is rendered</param>
78+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
79+
public UdpSink(string host, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
80+
: this(new SplunkUdpSinkConnectionInfo(host, port), formatProvider, renderTemplate)
81+
{
82+
}
83+
84+
/// <summary>
85+
/// Creates an instance of the Splunk UDP Sink
86+
/// </summary>
87+
/// <param name="hostAddress">The Splunk Host</param>
88+
/// <param name="port">The UDP port configured in Splunk</param>
89+
/// <param name="formatProvider">Optional format provider</param>
90+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
91+
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
92+
public UdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
93+
: this(new SplunkUdpSinkConnectionInfo(hostAddress, port), formatProvider, renderTemplate)
94+
{
95+
}
96+
97+
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
98+
{
99+
return new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
100+
}
101+
102+
/// <inheritdoc/>
103+
public void Emit(LogEvent logEvent)
104+
{
105+
var sb = new StringBuilder();
106+
107+
using (var sw = new StringWriter(sb))
108+
_formatter.Format(logEvent, sw);
109+
110+
_socket.Send(Encoding.UTF8.GetBytes(sb.ToString()));
111+
}
112+
113+
/// <inheritdoc/>
114+
public void Dispose()
115+
{
116+
_socket?.Close();
117+
_socket?.Dispose();
118+
_socket = null;
119+
}
120+
}
121+
122+
/// <summary>
123+
/// Defines connection info used to connect against Splunk
124+
/// using UDP.
125+
/// </summary>
126+
public class SplunkUdpSinkConnectionInfo
127+
{
128+
/// <summary>
129+
/// Splunk host.
130+
/// </summary>
131+
public IPAddress Host { get; }
132+
133+
/// <summary>
134+
/// Splunk port.
135+
/// </summary>
136+
public int Port { get; }
137+
138+
/// <summary>
139+
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
140+
/// for defining connection info for connecting using UDP against Splunk.
141+
/// </summary>
142+
/// <param name="host">Splunk host.</param>
143+
/// <param name="port">Splunk UDP port.</param>
144+
public SplunkUdpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port) { }
145+
146+
/// <summary>
147+
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
148+
/// for defining connection info for connecting using UDP against Splunk.
149+
/// </summary>
150+
/// <param name="host">Splunk host.</param>
151+
/// <param name="port">Splunk UDP port.</param>
152+
public SplunkUdpSinkConnectionInfo(IPAddress host, int port)
153+
{
154+
Host = host;
155+
Port = port;
156+
}
157+
}
158+
}
159+
160+
#endif

0 commit comments

Comments
 (0)