Skip to content

Commit

Permalink
Merge pull request #22 from merbla/dev
Browse files Browse the repository at this point in the history
Merge of master to dev ready for 2.0 release.
  • Loading branch information
merbla authored Jul 1, 2016
2 parents 1f702ff + 2f7cb65 commit cb1a8b0
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ if($LASTEXITCODE -ne 0) { exit 1 }


Pop-Location
Pop-Location
Pop-Location
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
##2.0
- Support for DotNet Core
- Event Collector fluent interface changed to `.WriteTo.EventCollector`
- Event Collector Sink targeting core
- TCP/UDP Sinks targeting 4.5 *ONLY*
- Changed HTTP Client to include URI endpoint to host: "services/collector/event"
- Updated Event Collector HTTP Client to add URI endpoint to host: "services/collector" if not included.
- Event Collector changed to use epoch time [#15](https://github.com/serilog/serilog-sinks-splunk/pull/15)

##1.8
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ Using the new Event Collector in Splunk 6.3
var log = new LoggerConfiguration()
.WriteTo.EventCollector("https://mysplunk:8088/services/collector", "myeventcollectortoken")
.CreateLogger();
```
```

More information is available [here](https://github.com/serilog/serilog-sinks-splunk/wiki).
51 changes: 40 additions & 11 deletions sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Sample
{
public class Program
{
public static string EventCollectorToken = "D7E04CDB-71A8-4266-90A1-90EF1620AA4B";
public static string EventCollectorToken = "04B42E81-100E-4BED-8AE9-FC5EE4E08602";

public static void Main(string[] args)
{
Expand All @@ -17,30 +17,48 @@ public static void Main(string[] args)
eventsToCreate = int.Parse(args[0]);

Log.Information("Sample starting up");
Serilog.Debugging.SelfLog.Out = System.Console.Out;

// Vanilla Tests
// Vanilla Tests just host
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.SplunkViaEventCollector(
.WriteTo.EventCollector(
"http://localhost:8088",
Program.EventCollectorToken)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla No services/collector")
.CreateLogger();

foreach (var i in Enumerable.Range(0, eventsToCreate))
{
Log.Information("Running vanilla without extended endpoint loop {Counter}", i);
Thread.Sleep(5);
}

// Vanilla Test with full uri specified
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088/services/collector",
Program.EventCollectorToken)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestCase", "Vanilla")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla with full uri specified")
.CreateLogger();

foreach (var i in Enumerable.Range(0, eventsToCreate))
{
Log.Information("Running vanilla loop {Counter}", i);
Thread.Sleep(5);
}

// Override Source
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.SplunkViaEventCollector(
"http://localhost:8088/services/collector",
.WriteTo.EventCollector(
"http://localhost:8088",
Program.EventCollectorToken,
source: "Serilog.Sinks.Splunk.Sample")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
Expand All @@ -57,8 +75,8 @@ public static void Main(string[] args)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.SplunkViaEventCollector(
"http://localhost:8088/services/collector",
.WriteTo.EventCollector(
"http://localhost:8088",
Program.EventCollectorToken,
host: "myamazingmachine")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
Expand All @@ -75,8 +93,8 @@ public static void Main(string[] args)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.SplunkViaEventCollector(
"http://localhost:8088/services/collector",
.WriteTo.EventCollector(
"http://localhost:8088",
Program.EventCollectorToken,
renderTemplate: false)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
Expand All @@ -88,6 +106,17 @@ public static void Main(string[] args)
Log.Information("Running no template loop {Counter}", i);
Thread.Sleep(5);
}

// SSL
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"https://localhost:8088",
Program.EventCollectorToken)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "HTTPS")
.CreateLogger();

Log.Debug("Waiting for Events to Flush");
Thread.Sleep(5000);
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public static double ToEpoch(this DateTimeOffset value)
return Math.Round((value - Epoch).TotalSeconds, 3, MidpointRounding.AwayFromZero);
}
}
}
}
10 changes: 8 additions & 2 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ public string Payload

internal class EventCollectorRequest : HttpRequestMessage
{
internal EventCollectorRequest(string splunkHost, string jsonPayLoad)
internal EventCollectorRequest(string splunkHost, string jsonPayLoad, string uri ="services/collector")
{
var hostUrl = $@"{splunkHost}/{uri}";

if(splunkHost.Contains("services/collector"))
{
hostUrl = $@"{splunkHost}";
}

var stringContent = new StringContent(jsonPayLoad, Encoding.UTF8, "application/json");
RequestUri = new Uri(splunkHost);
RequestUri = new Uri(hostUrl);
Content = stringContent;
Method = HttpMethod.Post;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
private readonly string _sourceType;
private readonly string _host;
private readonly string _index;
private readonly string _uriPath;
private readonly int _batchSizeLimitLimit;
private readonly SplunkJsonFormatter _jsonFormatter;
private readonly ConcurrentQueue<LogEvent> _queue;
Expand Down Expand Up @@ -97,6 +98,7 @@ public EventCollectorSink(
/// </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="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
Expand All @@ -108,6 +110,7 @@ public EventCollectorSink(
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
string source,
string sourceType,
string host,
Expand All @@ -127,6 +130,7 @@ public EventCollectorSink(
_sourceType = sourceType;
_host = host;
_index = index;
_uriPath = uriPath;
}

/// <summary>
Expand Down Expand Up @@ -185,7 +189,7 @@ private async Task Send(IEnumerable<LogEvent> events)
allEvents = $"{allEvents}{splunkEvent.Payload}";
}

var request = new EventCollectorRequest(_splunkHost, allEvents);
var request = new EventCollectorRequest(_splunkHost, allEvents, _uriPath);
var response = await _httpClient.SendAsync(request);

if (response.IsSuccessStatusCode)
Expand Down Expand Up @@ -240,4 +244,4 @@ protected virtual void Dispose(bool disposing)
_httpClient.Dispose();
}
}
}
}
43 changes: 4 additions & 39 deletions src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,7 @@ public static class SplunkLoggingConfigurationExtensions
/// <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="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="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If ture, the message template will be rendered</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>
/// <returns></returns>
public static LoggerConfiguration EventCollector(
this LoggerSinkConfiguration configuration,
string splunkHost,
string eventCollectorToken,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultOutputTemplate,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
int batchIntervalInSeconds = 2,
int batchSizeLimit = 100)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));

var eventCollectorSink = new EventCollectorSink(
splunkHost,
eventCollectorToken,
batchIntervalInSeconds,
batchSizeLimit,
formatProvider,
renderTemplate);

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="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="source">The source of the event</param>
/// <param name="sourceType">The source type of the event</param>
Expand All @@ -89,10 +52,11 @@ public static LoggerConfiguration EventCollector(
/// <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>
/// <returns></returns>
public static LoggerConfiguration SplunkViaEventCollector(
public static LoggerConfiguration EventCollector(
this LoggerSinkConfiguration configuration,
string splunkHost,
string eventCollectorToken,
string uriPath = "services/collector",
string source = DefaultSource,
string sourceType = DefaultSourceType,
string host = DefaultHost,
Expand All @@ -110,6 +74,7 @@ public static LoggerConfiguration SplunkViaEventCollector(
var eventCollectorSink = new EventCollectorSink(
splunkHost,
eventCollectorToken,
uriPath,
source,
sourceType,
host,
Expand Down

0 comments on commit cb1a8b0

Please sign in to comment.