diff --git a/Build.ps1 b/Build.ps1 index 0416c21..d28c393 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -13,4 +13,4 @@ if($LASTEXITCODE -ne 0) { exit 1 } Pop-Location -Pop-Location \ No newline at end of file +Pop-Location diff --git a/CHANGES.md b/CHANGES.md index 69f4a7b..b2dd806 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/README.md b/README.md index d14b649..1588fd2 100644 --- a/README.md +++ b/README.md @@ -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(); -``` \ No newline at end of file +``` + +More information is available [here](https://github.com/serilog/serilog-sinks-splunk/wiki). \ No newline at end of file diff --git a/sample/Program.cs b/sample/Program.cs index 665416d..8ac2af5 100755 --- a/sample/Program.cs +++ b/sample/Program.cs @@ -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) { @@ -17,16 +17,34 @@ 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)) @@ -34,13 +52,13 @@ public static void Main(string[] args) 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") @@ -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") @@ -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") @@ -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); diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs index 448816c..0f4c39b 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs @@ -31,4 +31,4 @@ public static double ToEpoch(this DateTimeOffset value) return Math.Round((value - Epoch).TotalSeconds, 3, MidpointRounding.AwayFromZero); } } -} \ No newline at end of file +} diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs index c5d4375..e4667e1 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs @@ -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; } diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs index 5213284..ee9f10d 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs @@ -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 _queue; @@ -97,6 +98,7 @@ public EventCollectorSink( /// /// 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 format provider used when rendering the message /// Whether to render the message template @@ -108,6 +110,7 @@ public EventCollectorSink( public EventCollectorSink( string splunkHost, string eventCollectorToken, + string uriPath, string source, string sourceType, string host, @@ -127,6 +130,7 @@ public EventCollectorSink( _sourceType = sourceType; _host = host; _index = index; + _uriPath = uriPath; } /// @@ -185,7 +189,7 @@ private async Task Send(IEnumerable 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) @@ -240,4 +244,4 @@ protected virtual void Dispose(bool disposing) _httpClient.Dispose(); } } -} \ No newline at end of file +} diff --git a/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs b/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs index 3c672f1..3db143b 100644 --- a/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs +++ b/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs @@ -40,44 +40,7 @@ public static class SplunkLoggingConfigurationExtensions /// The logger config /// The Splunk host that is configured with an Event Collector /// The token provided to authenticate to the Splunk Event Collector - /// The minimum log event level required in order to write an event to the sink. - /// The output template to be used when logging - /// Supplies culture-specific formatting information, or null. - /// If ture, the message template will be rendered - /// The interval in seconds that the queue should be instpected for batching - /// The size of the batch - /// - 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); - } - - /// - /// 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 + /// Change the default endpoint of the Event Collector e.g. services/collector/event /// The Splunk index to log to /// The source of the event /// The source type of the event @@ -89,10 +52,11 @@ public static LoggerConfiguration EventCollector( /// The interval in seconds that the queue should be instpected for batching /// The size of the batch /// - 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, @@ -110,6 +74,7 @@ public static LoggerConfiguration SplunkViaEventCollector( var eventCollectorSink = new EventCollectorSink( splunkHost, eventCollectorToken, + uriPath, source, sourceType, host,