From dbdc1fcf41f66da5e5497e0fc224efcaa0b40043 Mon Sep 17 00:00:00 2001 From: Diego Frata Date: Thu, 26 Jan 2017 10:59:55 +0000 Subject: [PATCH] Make it possible to override the text formatter implementation --- .../Sinks/Splunk/EventCollectorSink.cs | 39 +++++++++++++++-- .../SplunkLoggingConfigurationExtensions.cs | 43 +++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs index 1e86e19..0bad503 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs @@ -24,6 +24,7 @@ using Serilog.Core; using Serilog.Debugging; using Serilog.Events; +using Serilog.Formatting; namespace Serilog.Sinks.Splunk { @@ -35,7 +36,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable private readonly string _splunkHost; private readonly string _uriPath; private readonly int _batchSizeLimitLimit; - private readonly SplunkJsonFormatter _jsonFormatter; + private readonly ITextFormatter _jsonFormatter; private readonly ConcurrentQueue _queue; private readonly EventCollectorClient _httpClient; @@ -68,8 +69,8 @@ public EventCollectorSink( : this( splunkHost, eventCollectorToken, - null, null, null, null, null, - batchIntervalInSeconds, + null, null, null, null, null, + batchIntervalInSeconds, batchSizeLimit, formatProvider, renderTemplate) @@ -104,11 +105,41 @@ public EventCollectorSink( IFormatProvider formatProvider = null, bool renderTemplate = true, HttpMessageHandler messageHandler = null) + : this( + splunkHost, + eventCollectorToken, + uriPath, + batchIntervalInSeconds, + batchSizeLimit, + new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index), + messageHandler) + { + } + + + /// + /// Creates a new instance of the sink + /// + /// The host of the Splunk instance with the Event collector configured + /// The token to use when authenticating with the event collector + /// Change the default endpoint of the Event Collector e.g. services/collector/event + /// The size of the batch when sending to the event collector + /// The interval in seconds that batching should occur + /// The text formatter used to render log events into a JSON format for consumption by Splunk + /// The handler used to send HTTP requests + public EventCollectorSink( + string splunkHost, + string eventCollectorToken, + string uriPath, + int batchIntervalInSeconds, + int batchSizeLimit, + ITextFormatter jsonFormatter, + HttpMessageHandler messageHandler = null) { _uriPath = uriPath; _splunkHost = splunkHost; _queue = new ConcurrentQueue(); - _jsonFormatter = new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index); + _jsonFormatter = jsonFormatter; _batchSizeLimitLimit = batchSizeLimit; var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds); diff --git a/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs b/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs index b14dec1..7551d41 100644 --- a/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs +++ b/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs @@ -17,6 +17,7 @@ using System.Net.Http; using Serilog.Configuration; using Serilog.Events; +using Serilog.Formatting; using Serilog.Sinks.Splunk; namespace Serilog @@ -89,5 +90,47 @@ public static LoggerConfiguration EventCollector( return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel); } + + /// + /// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector. + /// + /// The logger config + /// The Splunk host that is configured with an Event Collector + /// The token provided to authenticate to the Splunk Event Collector + /// The text formatter used to render log events into a JSON format for consumption by Splunk + /// Change the default endpoint of the Event Collector e.g. services/collector/event + /// The minimum log event level required in order to write an event to the sink. + /// The output template to be used when logging + /// The interval in seconds that the queue should be instpected for batching + /// The size of the batch + /// The handler used to send HTTP requests + /// + public static LoggerConfiguration EventCollector( + this LoggerSinkConfiguration configuration, + string splunkHost, + string eventCollectorToken, + ITextFormatter jsonFormatter, + string uriPath = "services/collector", + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, + string outputTemplate = DefaultOutputTemplate, + int batchIntervalInSeconds = 2, + int batchSizeLimit = 100, + HttpMessageHandler messageHandler = null) + { + if (configuration == null) throw new ArgumentNullException(nameof(configuration)); + if (jsonFormatter == null) throw new ArgumentNullException(nameof(jsonFormatter)); + if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); + + var eventCollectorSink = new EventCollectorSink( + splunkHost, + eventCollectorToken, + uriPath, + batchIntervalInSeconds, + batchSizeLimit, + jsonFormatter, + messageHandler); + + return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel); + } } } \ No newline at end of file