Skip to content

Commit 6222e2c

Browse files
authored
Merge pull request #43 from diegofrata/sink-extensibility
Make it possible to override the text formatter implementation
2 parents 4468cea + dbdc1fc commit 6222e2c

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Serilog.Core;
2525
using Serilog.Debugging;
2626
using Serilog.Events;
27+
using Serilog.Formatting;
2728

2829
namespace Serilog.Sinks.Splunk
2930
{
@@ -35,7 +36,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
3536
private readonly string _splunkHost;
3637
private readonly string _uriPath;
3738
private readonly int _batchSizeLimitLimit;
38-
private readonly SplunkJsonFormatter _jsonFormatter;
39+
private readonly ITextFormatter _jsonFormatter;
3940
private readonly ConcurrentQueue<LogEvent> _queue;
4041
private readonly EventCollectorClient _httpClient;
4142

@@ -68,8 +69,8 @@ public EventCollectorSink(
6869
: this(
6970
splunkHost,
7071
eventCollectorToken,
71-
null, null, null, null, null,
72-
batchIntervalInSeconds,
72+
null, null, null, null, null,
73+
batchIntervalInSeconds,
7374
batchSizeLimit,
7475
formatProvider,
7576
renderTemplate)
@@ -104,11 +105,41 @@ public EventCollectorSink(
104105
IFormatProvider formatProvider = null,
105106
bool renderTemplate = true,
106107
HttpMessageHandler messageHandler = null)
108+
: this(
109+
splunkHost,
110+
eventCollectorToken,
111+
uriPath,
112+
batchIntervalInSeconds,
113+
batchSizeLimit,
114+
new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index),
115+
messageHandler)
116+
{
117+
}
118+
119+
120+
/// <summary>
121+
/// Creates a new instance of the sink
122+
/// </summary>
123+
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
124+
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
125+
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
126+
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
127+
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
128+
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
129+
/// <param name="messageHandler">The handler used to send HTTP requests</param>
130+
public EventCollectorSink(
131+
string splunkHost,
132+
string eventCollectorToken,
133+
string uriPath,
134+
int batchIntervalInSeconds,
135+
int batchSizeLimit,
136+
ITextFormatter jsonFormatter,
137+
HttpMessageHandler messageHandler = null)
107138
{
108139
_uriPath = uriPath;
109140
_splunkHost = splunkHost;
110141
_queue = new ConcurrentQueue<LogEvent>();
111-
_jsonFormatter = new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index);
142+
_jsonFormatter = jsonFormatter;
112143
_batchSizeLimitLimit = batchSizeLimit;
113144

114145
var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);

src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Net.Http;
1818
using Serilog.Configuration;
1919
using Serilog.Events;
20+
using Serilog.Formatting;
2021
using Serilog.Sinks.Splunk;
2122

2223
namespace Serilog
@@ -89,5 +90,47 @@ public static LoggerConfiguration EventCollector(
8990

9091
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
9192
}
93+
94+
/// <summary>
95+
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
96+
/// </summary>
97+
/// <param name="configuration">The logger config</param>
98+
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
99+
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
100+
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
101+
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
102+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
103+
/// <param name="outputTemplate">The output template to be used when logging</param>
104+
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
105+
/// <param name="batchSizeLimit">The size of the batch</param>
106+
/// <param name="messageHandler">The handler used to send HTTP requests</param>
107+
/// <returns></returns>
108+
public static LoggerConfiguration EventCollector(
109+
this LoggerSinkConfiguration configuration,
110+
string splunkHost,
111+
string eventCollectorToken,
112+
ITextFormatter jsonFormatter,
113+
string uriPath = "services/collector",
114+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
115+
string outputTemplate = DefaultOutputTemplate,
116+
int batchIntervalInSeconds = 2,
117+
int batchSizeLimit = 100,
118+
HttpMessageHandler messageHandler = null)
119+
{
120+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
121+
if (jsonFormatter == null) throw new ArgumentNullException(nameof(jsonFormatter));
122+
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
123+
124+
var eventCollectorSink = new EventCollectorSink(
125+
splunkHost,
126+
eventCollectorToken,
127+
uriPath,
128+
batchIntervalInSeconds,
129+
batchSizeLimit,
130+
jsonFormatter,
131+
messageHandler);
132+
133+
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
134+
}
92135
}
93136
}

0 commit comments

Comments
 (0)