Skip to content

Commit eccb1d7

Browse files
committed
Merge pull request #19 from merbla/fix-overloads
Corrected overloads for conf. Added sample.
2 parents fa30c2d + 4754050 commit eccb1d7

File tree

6 files changed

+164
-8
lines changed

6 files changed

+164
-8
lines changed

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ dotnet restore
33
for path in src/*/project.json; do
44
dirname="$(dirname "${path}")"
55
dotnet build ${dirname} -c Release
6+
done
7+
8+
for path in sample/project.json; do
9+
dirname="$(dirname "${path}")"
10+
dotnet build ${dirname} -c Release
611
done

run_sample.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
sh build.sh
3+
4+
cd sample
5+
dotnet run 15
6+
cd ..

sample/Program.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Linq;
2+
using System.Threading;
3+
using Serilog;
4+
using Serilog.Core;
5+
6+
namespace Sample
7+
{
8+
public class Program
9+
{
10+
public static string EventCollectorToken = "D7E04CDB-71A8-4266-90A1-90EF1620AA4B";
11+
12+
public static void Main(string[] args)
13+
{
14+
var eventsToCreate = 100;
15+
16+
if(args.Length > 0)
17+
eventsToCreate = int.Parse(args[0]);
18+
19+
Log.Information("Sample starting up");
20+
21+
// Vanilla Tests
22+
Log.Logger = new LoggerConfiguration()
23+
.MinimumLevel.Debug()
24+
.WriteTo.LiterateConsole()
25+
.WriteTo.SplunkViaEventCollector(
26+
"http://localhost:8088/services/collector",
27+
Program.EventCollectorToken)
28+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
29+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestCase", "Vanilla")
30+
.CreateLogger();
31+
32+
foreach (var i in Enumerable.Range(0, eventsToCreate))
33+
{
34+
Log.Information("Running vanilla loop {Counter}", i);
35+
Thread.Sleep(5);
36+
}
37+
38+
// Override Source
39+
Log.Logger = new LoggerConfiguration()
40+
.MinimumLevel.Debug()
41+
.WriteTo.LiterateConsole()
42+
.WriteTo.SplunkViaEventCollector(
43+
"http://localhost:8088/services/collector",
44+
Program.EventCollectorToken,
45+
source: "Serilog.Sinks.Splunk.Sample")
46+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
47+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Source Override")
48+
.CreateLogger();
49+
50+
foreach (var i in Enumerable.Range(0, eventsToCreate))
51+
{
52+
Log.Information("Running source override loop {Counter}", i);
53+
Thread.Sleep(5);
54+
}
55+
56+
// Override Host
57+
Log.Logger = new LoggerConfiguration()
58+
.MinimumLevel.Debug()
59+
.WriteTo.LiterateConsole()
60+
.WriteTo.SplunkViaEventCollector(
61+
"http://localhost:8088/services/collector",
62+
Program.EventCollectorToken,
63+
host: "myamazingmachine")
64+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
65+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Host Override")
66+
.CreateLogger();
67+
68+
foreach (var i in Enumerable.Range(0, eventsToCreate))
69+
{
70+
Log.Information("Running host override loop {Counter}", i);
71+
Thread.Sleep(5);
72+
}
73+
74+
// No Template
75+
Log.Logger = new LoggerConfiguration()
76+
.MinimumLevel.Debug()
77+
.WriteTo.LiterateConsole()
78+
.WriteTo.SplunkViaEventCollector(
79+
"http://localhost:8088/services/collector",
80+
Program.EventCollectorToken,
81+
renderTemplate: false)
82+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
83+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "No Templates")
84+
.CreateLogger();
85+
86+
foreach (var i in Enumerable.Range(0, eventsToCreate))
87+
{
88+
Log.Information("Running no template loop {Counter}", i);
89+
Thread.Sleep(5);
90+
}
91+
92+
Log.Debug("Waiting for Events to Flush");
93+
Thread.Sleep(5000);
94+
Log.Debug("Done");
95+
96+
}
97+
}
98+
}

sample/project.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "2.0.0-rc-*",
3+
"buildOptions": {
4+
"emitEntryPoint": true
5+
},
6+
"dependencies": {
7+
"Microsoft.NETCore.App": {
8+
"type": "platform",
9+
"version": "1.0.0-rc2-3002702"
10+
},
11+
"Serilog.Sinks.Splunk": {"target": "project"},
12+
"Serilog.Sinks.Literate": "2.0.0-rc-25"
13+
},
14+
"frameworks": {
15+
"netcoreapp1.0": {
16+
"imports": "dnxcore50"
17+
}
18+
}
19+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public class EventCollectorSink : ILogEventSink, IDisposable
4242
private readonly SplunkJsonFormatter _jsonFormatter;
4343
private readonly ConcurrentQueue<LogEvent> _queue;
4444
private readonly EventCollectorClient _httpClient;
45+
private const string DefaultSource = "";
46+
private const string DefaultSourceType = "";
47+
private const string DefaultHost = "";
48+
private const string DefaultIndex = "";
4549

4650
/// <summary>
4751
/// Taken from Splunk.Logging.Common
@@ -78,13 +82,10 @@ public EventCollectorSink(
7882
_batchSizeLimitLimit = batchSizeLimit;
7983

8084
var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);
81-
8285
_httpClient = new EventCollectorClient(_eventCollectorToken);
8386

8487
var cancellationToken = new CancellationToken();
85-
86-
// _timer = new PortableTimer(async c => await ProcessQueue());
87-
88+
8889
RepeatAction.OnInterval(
8990
batchInterval,
9091
async () => await ProcessQueue(),
@@ -111,8 +112,8 @@ public EventCollectorSink(
111112
string sourceType,
112113
string host,
113114
string index,
114-
int batchIntervalInSeconds = 5,
115-
int batchSizeLimit = 100,
115+
int batchIntervalInSeconds,
116+
int batchSizeLimit,
116117
IFormatProvider formatProvider = null,
117118
bool renderTemplate = true
118119
) : this(splunkHost,

src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public static class SplunkLoggingConfigurationExtensions
2828
{
2929
internal const string DefaultOutputTemplate =
3030
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
31+
32+
internal const string DefaultSource = "";
33+
internal const string DefaultSourceType = "";
34+
internal const string DefaultHost = "";
35+
internal const string DefaultIndex = "";
3136

3237
/// <summary>
3338
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
@@ -73,6 +78,10 @@ public static LoggerConfiguration EventCollector(
7378
/// <param name="configuration">The logger config</param>
7479
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
7580
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
81+
/// <param name="index">The Splunk index to log to</param>
82+
/// <param name="source">The source of the event</param>
83+
/// <param name="sourceType">The source type of the event</param>
84+
/// <param name="host">The host of the event</param>
7685
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
7786
/// <param name="outputTemplate">The output template to be used when logging</param>
7887
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
@@ -84,15 +93,33 @@ public static LoggerConfiguration SplunkViaEventCollector(
8493
this LoggerSinkConfiguration configuration,
8594
string splunkHost,
8695
string eventCollectorToken,
96+
string source = DefaultSource,
97+
string sourceType = DefaultSourceType,
98+
string host = DefaultHost,
99+
string index = DefaultIndex,
87100
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
88101
string outputTemplate = DefaultOutputTemplate,
89102
IFormatProvider formatProvider = null,
90103
bool renderTemplate = true,
91104
int batchIntervalInSeconds = 2,
92105
int batchSizeLimit = 100)
93106
{
94-
return EventCollector(configuration, splunkHost, eventCollectorToken, restrictedToMinimumLevel,
95-
outputTemplate, formatProvider, renderTemplate, batchIntervalInSeconds, batchSizeLimit);
107+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
108+
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
109+
110+
var eventCollectorSink = new EventCollectorSink(
111+
splunkHost,
112+
eventCollectorToken,
113+
source,
114+
sourceType,
115+
host,
116+
index,
117+
batchIntervalInSeconds,
118+
batchSizeLimit,
119+
formatProvider,
120+
renderTemplate);
121+
122+
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
96123
}
97124
}
98125
}

0 commit comments

Comments
 (0)