Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to override the text formatter implementation #43

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Formatting;

namespace Serilog.Sinks.Splunk
{
Expand All @@ -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<LogEvent> _queue;
private readonly EventCollectorClient _httpClient;

Expand Down Expand Up @@ -68,8 +69,8 @@ public EventCollectorSink(
: this(
splunkHost,
eventCollectorToken,
null, null, null, null, null,
batchIntervalInSeconds,
null, null, null, null, null,
batchIntervalInSeconds,
batchSizeLimit,
formatProvider,
renderTemplate)
Expand Down Expand Up @@ -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)
{
}


/// <summary>
/// Creates a new instance of the sink
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
int batchIntervalInSeconds,
int batchSizeLimit,
ITextFormatter jsonFormatter,
HttpMessageHandler messageHandler = null)
{
_uriPath = uriPath;
_splunkHost = splunkHost;
_queue = new ConcurrentQueue<LogEvent>();
_jsonFormatter = new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index);
_jsonFormatter = jsonFormatter;
_batchSizeLimitLimit = batchSizeLimit;

var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);
Expand Down
43 changes: 43 additions & 0 deletions src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Net.Http;
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.Splunk;

namespace Serilog
Expand Down Expand Up @@ -89,5 +90,47 @@ public static LoggerConfiguration EventCollector(

return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
}

/// <summary>
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
/// </summary>
/// <param name="configuration">The logger config</param>
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="outputTemplate">The output template to be used when logging</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
/// <param name="batchSizeLimit">The size of the batch</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <returns></returns>
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);
}
}
}