Skip to content

Commit cb1a8b0

Browse files
authored
Merge pull request #22 from merbla/dev
Merge of master to dev ready for 2.0 release.
2 parents 1f702ff + 2f7cb65 commit cb1a8b0

File tree

8 files changed

+65
-58
lines changed

8 files changed

+65
-58
lines changed

Build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ if($LASTEXITCODE -ne 0) { exit 1 }
1313

1414

1515
Pop-Location
16-
Pop-Location
16+
Pop-Location

CHANGES.md

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

89
##1.8

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ Using the new Event Collector in Splunk 6.3
2323
var log = new LoggerConfiguration()
2424
.WriteTo.EventCollector("https://mysplunk:8088/services/collector", "myeventcollectortoken")
2525
.CreateLogger();
26-
```
26+
```
27+
28+
More information is available [here](https://github.com/serilog/serilog-sinks-splunk/wiki).

sample/Program.cs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Sample
77
{
88
public class Program
99
{
10-
public static string EventCollectorToken = "D7E04CDB-71A8-4266-90A1-90EF1620AA4B";
10+
public static string EventCollectorToken = "04B42E81-100E-4BED-8AE9-FC5EE4E08602";
1111

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

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

21-
// Vanilla Tests
22+
// Vanilla Tests just host
2223
Log.Logger = new LoggerConfiguration()
2324
.MinimumLevel.Debug()
2425
.WriteTo.LiterateConsole()
25-
.WriteTo.SplunkViaEventCollector(
26+
.WriteTo.EventCollector(
27+
"http://localhost:8088",
28+
Program.EventCollectorToken)
29+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
30+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla No services/collector")
31+
.CreateLogger();
32+
33+
foreach (var i in Enumerable.Range(0, eventsToCreate))
34+
{
35+
Log.Information("Running vanilla without extended endpoint loop {Counter}", i);
36+
Thread.Sleep(5);
37+
}
38+
39+
// Vanilla Test with full uri specified
40+
Log.Logger = new LoggerConfiguration()
41+
.MinimumLevel.Debug()
42+
.WriteTo.LiterateConsole()
43+
.WriteTo.EventCollector(
2644
"http://localhost:8088/services/collector",
2745
Program.EventCollectorToken)
2846
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
29-
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestCase", "Vanilla")
47+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla with full uri specified")
3048
.CreateLogger();
3149

3250
foreach (var i in Enumerable.Range(0, eventsToCreate))
3351
{
3452
Log.Information("Running vanilla loop {Counter}", i);
3553
Thread.Sleep(5);
3654
}
37-
55+
3856
// Override Source
3957
Log.Logger = new LoggerConfiguration()
4058
.MinimumLevel.Debug()
4159
.WriteTo.LiterateConsole()
42-
.WriteTo.SplunkViaEventCollector(
43-
"http://localhost:8088/services/collector",
60+
.WriteTo.EventCollector(
61+
"http://localhost:8088",
4462
Program.EventCollectorToken,
4563
source: "Serilog.Sinks.Splunk.Sample")
4664
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
@@ -57,8 +75,8 @@ public static void Main(string[] args)
5775
Log.Logger = new LoggerConfiguration()
5876
.MinimumLevel.Debug()
5977
.WriteTo.LiterateConsole()
60-
.WriteTo.SplunkViaEventCollector(
61-
"http://localhost:8088/services/collector",
78+
.WriteTo.EventCollector(
79+
"http://localhost:8088",
6280
Program.EventCollectorToken,
6381
host: "myamazingmachine")
6482
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
@@ -75,8 +93,8 @@ public static void Main(string[] args)
7593
Log.Logger = new LoggerConfiguration()
7694
.MinimumLevel.Debug()
7795
.WriteTo.LiterateConsole()
78-
.WriteTo.SplunkViaEventCollector(
79-
"http://localhost:8088/services/collector",
96+
.WriteTo.EventCollector(
97+
"http://localhost:8088",
8098
Program.EventCollectorToken,
8199
renderTemplate: false)
82100
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
@@ -88,6 +106,17 @@ public static void Main(string[] args)
88106
Log.Information("Running no template loop {Counter}", i);
89107
Thread.Sleep(5);
90108
}
109+
110+
// SSL
111+
Log.Logger = new LoggerConfiguration()
112+
.MinimumLevel.Debug()
113+
.WriteTo.LiterateConsole()
114+
.WriteTo.EventCollector(
115+
"https://localhost:8088",
116+
Program.EventCollectorToken)
117+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
118+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "HTTPS")
119+
.CreateLogger();
91120

92121
Log.Debug("Waiting for Events to Flush");
93122
Thread.Sleep(5000);

src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ public static double ToEpoch(this DateTimeOffset value)
3131
return Math.Round((value - Epoch).TotalSeconds, 3, MidpointRounding.AwayFromZero);
3232
}
3333
}
34-
}
34+
}

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ public string Payload
6565

6666
internal class EventCollectorRequest : HttpRequestMessage
6767
{
68-
internal EventCollectorRequest(string splunkHost, string jsonPayLoad)
68+
internal EventCollectorRequest(string splunkHost, string jsonPayLoad, string uri ="services/collector")
6969
{
70+
var hostUrl = $@"{splunkHost}/{uri}";
7071

72+
if(splunkHost.Contains("services/collector"))
73+
{
74+
hostUrl = $@"{splunkHost}";
75+
}
76+
7177
var stringContent = new StringContent(jsonPayLoad, Encoding.UTF8, "application/json");
72-
RequestUri = new Uri(splunkHost);
78+
RequestUri = new Uri(hostUrl);
7379
Content = stringContent;
7480
Method = HttpMethod.Post;
7581
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
3838
private readonly string _sourceType;
3939
private readonly string _host;
4040
private readonly string _index;
41+
private readonly string _uriPath;
4142
private readonly int _batchSizeLimitLimit;
4243
private readonly SplunkJsonFormatter _jsonFormatter;
4344
private readonly ConcurrentQueue<LogEvent> _queue;
@@ -97,6 +98,7 @@ public EventCollectorSink(
9798
/// </summary>
9899
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
99100
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
101+
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
100102
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
101103
/// <param name="formatProvider">The format provider used when rendering the message</param>
102104
/// <param name="renderTemplate">Whether to render the message template</param>
@@ -108,6 +110,7 @@ public EventCollectorSink(
108110
public EventCollectorSink(
109111
string splunkHost,
110112
string eventCollectorToken,
113+
string uriPath,
111114
string source,
112115
string sourceType,
113116
string host,
@@ -127,6 +130,7 @@ public EventCollectorSink(
127130
_sourceType = sourceType;
128131
_host = host;
129132
_index = index;
133+
_uriPath = uriPath;
130134
}
131135

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

188-
var request = new EventCollectorRequest(_splunkHost, allEvents);
192+
var request = new EventCollectorRequest(_splunkHost, allEvents, _uriPath);
189193
var response = await _httpClient.SendAsync(request);
190194

191195
if (response.IsSuccessStatusCode)
@@ -240,4 +244,4 @@ protected virtual void Dispose(bool disposing)
240244
_httpClient.Dispose();
241245
}
242246
}
243-
}
247+
}

src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,7 @@ public static class SplunkLoggingConfigurationExtensions
4040
/// <param name="configuration">The logger config</param>
4141
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
4242
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
43-
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
44-
/// <param name="outputTemplate">The output template to be used when logging</param>
45-
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
46-
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
47-
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
48-
/// <param name="batchSizeLimit">The size of the batch</param>
49-
/// <returns></returns>
50-
public static LoggerConfiguration EventCollector(
51-
this LoggerSinkConfiguration configuration,
52-
string splunkHost,
53-
string eventCollectorToken,
54-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
55-
string outputTemplate = DefaultOutputTemplate,
56-
IFormatProvider formatProvider = null,
57-
bool renderTemplate = true,
58-
int batchIntervalInSeconds = 2,
59-
int batchSizeLimit = 100)
60-
{
61-
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
62-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
63-
64-
var eventCollectorSink = new EventCollectorSink(
65-
splunkHost,
66-
eventCollectorToken,
67-
batchIntervalInSeconds,
68-
batchSizeLimit,
69-
formatProvider,
70-
renderTemplate);
71-
72-
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
73-
}
74-
75-
/// <summary>
76-
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
77-
/// </summary>
78-
/// <param name="configuration">The logger config</param>
79-
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
80-
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
43+
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
8144
/// <param name="index">The Splunk index to log to</param>
8245
/// <param name="source">The source of the event</param>
8346
/// <param name="sourceType">The source type of the event</param>
@@ -89,10 +52,11 @@ public static LoggerConfiguration EventCollector(
8952
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
9053
/// <param name="batchSizeLimit">The size of the batch</param>
9154
/// <returns></returns>
92-
public static LoggerConfiguration SplunkViaEventCollector(
55+
public static LoggerConfiguration EventCollector(
9356
this LoggerSinkConfiguration configuration,
9457
string splunkHost,
9558
string eventCollectorToken,
59+
string uriPath = "services/collector",
9660
string source = DefaultSource,
9761
string sourceType = DefaultSourceType,
9862
string host = DefaultHost,
@@ -110,6 +74,7 @@ public static LoggerConfiguration SplunkViaEventCollector(
11074
var eventCollectorSink = new EventCollectorSink(
11175
splunkHost,
11276
eventCollectorToken,
77+
uriPath,
11378
source,
11479
sourceType,
11580
host,

0 commit comments

Comments
 (0)