Skip to content

Commit 00c699e

Browse files
committed
Extensions for Event Collector overrides
New sink closes serilog/serilog#483
1 parent 7987c77 commit 00c699e

File tree

8 files changed

+227
-97
lines changed

8 files changed

+227
-97
lines changed

sample/Serilog.Sinks.Splunk.Sample/Program.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ static void Main(string[] args)
1313
;
1414
var tcp = new ViaTcp();
1515
var udp = new ViaUdp();
16-
var eventCollector = new ViaEventCollector();
17-
eventCollector.Configure();
16+
var ec = new ViaEventCollector();
17+
var eco = new ViaEventCollectorWithExtendedOptions();
18+
19+
eco.Configure();
20+
21+
//ec.Configure();
22+
1823
//udp.Configure();
1924
//tcp.Configure();
2025

@@ -26,7 +31,7 @@ static void Main(string[] args)
2631
}
2732
}
2833

29-
class ViaEventCollector : IConfigure
34+
class ViaEventCollector : IConfigure
3035
{
3136
public void Configure()
3237
{
@@ -36,12 +41,35 @@ public void Configure()
3641
.WriteTo.LiterateConsole()
3742
.WriteTo.SplunkViaEventCollector("https://mysplunk:8088/services/collector", "685546AE-0278-4786-97C4-5971676D5D70",renderTemplate:false)
3843
.Enrich.WithThreadId()
39-
.Enrich.WithProperty("SplunkSample", "ViaEventCollector")
44+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
4045
.MinimumLevel.Debug()
4146
.CreateLogger();
4247
}
4348
}
4449

50+
class ViaEventCollectorWithExtendedOptions : IConfigure
51+
{
52+
public void Configure()
53+
{
54+
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
55+
56+
Log.Logger = new LoggerConfiguration()
57+
.WriteTo.LiterateConsole()
58+
.WriteTo.SplunkViaEventCollector("https://mysplunk:8088/services/collector",
59+
"685546AE-0278-4786-97C4-5971676D5D70",
60+
"Serilog",
61+
"",
62+
Environment.MachineName,
63+
"" ,
64+
renderTemplate: false)
65+
.Enrich.WithThreadId()
66+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
67+
.MinimumLevel.Debug()
68+
.CreateLogger();
69+
}
70+
}
71+
72+
4573
class ViaTcp : IConfigure
4674
{
4775
public void Configure()
@@ -50,7 +78,7 @@ public void Configure()
5078
.WriteTo.LiterateConsole()
5179
.WriteTo.SplunkViaTcp("127.0.0.1", 10001, renderTemplate: false)
5280
.Enrich.WithThreadId()
53-
.Enrich.WithProperty("SplunkSample", "ViaTCP")
81+
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaTCP")
5482
.MinimumLevel.Debug()
5583
.CreateLogger();
5684
}

src/Serilog.Sinks.Splunk.FullNetFx/Serilog.Sinks.Splunk.FullNetFx.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@
6565
<Compile Include="..\Serilog.Sinks.Splunk\LoggerConfigurationSplunkPCLExtensions.cs">
6666
<Link>LoggerConfigurationSplunkPCLExtensions.cs</Link>
6767
</Compile>
68-
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\EventCollectorExtensions.cs">
69-
<Link>Sinks\Splunk\EventCollectorExtensions.cs</Link>
68+
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\EventCollectorClient.cs">
69+
<Link>Sinks\Splunk\EventCollectorClient.cs</Link>
70+
</Compile>
71+
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\EventCollectorRequest.cs">
72+
<Link>Sinks\Splunk\EventCollectorRequest.cs</Link>
7073
</Compile>
7174
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\EventCollectorSink.cs">
7275
<Link>Sinks\Splunk\EventCollectorSink.cs</Link>

src/Serilog.Sinks.Splunk/LoggerConfigurationSplunkPCLExtensions.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,54 @@ public static LoggerConfiguration SplunkViaEventCollector(
6767
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
6868
}
6969

70-
70+
/// <summary>
71+
/// Adds a sink that writes log events as to a Splunk instance via UDP.
72+
/// </summary>
73+
/// <param name="configuration">The logger config</param>
74+
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
75+
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
76+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
77+
/// <param name="outputTemplate">The output template to be used when logging</param>
78+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
79+
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
80+
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
81+
/// <param name="batchSizeLimit">The size of the batch</param>
82+
/// <param name="index">The Splunk index to log to</param>
83+
/// <param name="source">The source of the event</param>
84+
/// <param name="sourceType">The source type of the event</param>
85+
/// <param name="host">The host of the event</param>
86+
/// <returns></returns>
87+
public static LoggerConfiguration SplunkViaEventCollector(
88+
this LoggerSinkConfiguration configuration,
89+
string splunkHost,
90+
string eventCollectorToken,
91+
string source,
92+
string sourceType,
93+
string host,
94+
string index,
95+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
96+
string outputTemplate = DefaultOutputTemplate,
97+
IFormatProvider formatProvider = null,
98+
bool renderTemplate = true,
99+
int batchIntervalInSeconds = 2,
100+
int batchSizeLimit = 10)
101+
{
102+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
103+
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
104+
105+
var eventCollectorSink = new EventCollectorSink(
106+
splunkHost,
107+
eventCollectorToken,
108+
source,
109+
sourceType,
110+
host,
111+
index,
112+
batchIntervalInSeconds,
113+
batchSizeLimit,
114+
formatProvider,
115+
renderTemplate);
116+
117+
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
118+
}
71119
}
72120
}

src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@
5454
<Compile Include="..\..\assets\CommonAssemblyInfo.cs">
5555
<Link>Properties\CommonAssemblyInfo.cs</Link>
5656
</Compile>
57+
<Compile Include="Sinks\Splunk\EventCollectorClient.cs" />
58+
<Compile Include="Sinks\Splunk\EventCollectorRequest.cs" />
5759
<Compile Include="Sinks\Splunk\RepeatAction.cs" />
5860
<Compile Include="Sinks\Splunk\SplunkJsonFormatter.cs" />
59-
<Compile Include="Sinks\Splunk\EventCollectorExtensions.cs" />
6061
<Compile Include="Sinks\Splunk\EventCollectorSink.cs" />
6162
</ItemGroup>
6263
<ItemGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Net.Http.Headers;
4+
5+
namespace Serilog.Sinks.Splunk
6+
{
7+
internal class EventCollectorClient : HttpClient, IDisposable
8+
{
9+
public EventCollectorClient(string eventCollectorToken) : base()
10+
{
11+
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", eventCollectorToken);
12+
}
13+
}
14+
}

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorExtensions.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Text;
4+
5+
namespace Serilog.Sinks.Splunk
6+
{
7+
internal class EventCollectorRequest : HttpRequestMessage
8+
{
9+
public EventCollectorRequest(
10+
string splunkHost,
11+
string logEvent,
12+
string source,
13+
string sourceType,
14+
string host,
15+
string index)
16+
{
17+
18+
var jsonPayLoad = @"{""event"":" + logEvent
19+
.Replace("\r\n", string.Empty);
20+
21+
if (!string.IsNullOrWhiteSpace(source))
22+
{
23+
jsonPayLoad = jsonPayLoad + @",""source"":""" + source + @"""";
24+
}
25+
if (!string.IsNullOrWhiteSpace(sourceType))
26+
{
27+
jsonPayLoad = jsonPayLoad + @",""sourceType"":""" + sourceType + @"""";
28+
}
29+
if (!string.IsNullOrWhiteSpace(host))
30+
{
31+
jsonPayLoad = jsonPayLoad + @",""host"":""" + host + @"""";
32+
}
33+
if (!string.IsNullOrWhiteSpace(index))
34+
{
35+
jsonPayLoad = jsonPayLoad + @",""index"":""" + index + @"""";
36+
}
37+
38+
jsonPayLoad = jsonPayLoad + "}";
39+
40+
var stringContent = new StringContent(jsonPayLoad, Encoding.UTF8, "application/json");
41+
RequestUri = new Uri(splunkHost);
42+
Content = stringContent;
43+
Method = HttpMethod.Post;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)