Skip to content

Commit f1a0047

Browse files
committed
Changed to include Event Collector URI in the HTTP Request.
1 parent fada8df commit f1a0047

File tree

7 files changed

+92
-103
lines changed

7 files changed

+92
-103
lines changed

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
##2.0 Support for DotNet Core
1+
##2.0
2+
- Support for DotNet Core
23
- Event Collector Sink targeting core
34
- TCP/UDP Sinks targeting 4.5
5+
- Changed HTTP Client to include URI endpoint to host: "services/collector/event"
46

57
##1.7
68
- Better support for formatting including [#578](https://github.com/serilog/serilog/issues/578)

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ public EventCollectorClient(string eventCollectorToken) : base()
1010
{
1111
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", eventCollectorToken);
1212
}
13+
14+
public EventCollectorClient(string eventCollectorToken, HttpMessageHandler messageHandler) : base(messageHandler)
15+
{
16+
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", eventCollectorToken);
17+
}
1318
}
1419
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ internal class EventCollectorRequest : HttpRequestMessage
4646
{
4747
internal EventCollectorRequest(string splunkHost, string jsonPayLoad)
4848
{
49+
var hostUrl = $@"{splunkHost}/services/collector/event";
50+
4951
var stringContent = new StringContent(jsonPayLoad, Encoding.UTF8, "application/json");
50-
RequestUri = new Uri(splunkHost);
52+
RequestUri = new Uri(hostUrl);
5153
Content = stringContent;
5254
Method = HttpMethod.Post;
5355
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.IO;
2020
using System.Linq;
2121
using System.Net;
22+
using System.Net.Http;
2223
using System.Threading;
2324
using System.Threading.Tasks;
2425
using Serilog.Core;
@@ -52,8 +53,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
5253
HttpStatusCode.Forbidden,
5354
HttpStatusCode.MethodNotAllowed,
5455
HttpStatusCode.BadRequest
55-
};
56-
56+
};
5757

5858
/// <summary>
5959
/// Creates a new instance of the sink
@@ -86,14 +86,12 @@ public EventCollectorSink(
8686
var cancellationToken = new CancellationToken();
8787

8888
// _timer = new PortableTimer(async c => await ProcessQueue());
89-
90-
91-
89+
9290
RepeatAction.OnInterval(
9391
batchInterval,
9492
async () => await ProcessQueue(),
9593
cancellationToken);
96-
}
94+
}
9795

9896
/// <summary>
9997
/// Creates a new instance of the sink

src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,88 +14,85 @@
1414

1515

1616
using System;
17-
using System.Runtime.CompilerServices;
17+
using System.Net.Http;
1818
using Serilog.Configuration;
1919
using Serilog.Events;
2020
using Serilog.Sinks.Splunk;
2121

2222
namespace Serilog
2323
{
2424
/// <summary>
25-
/// Fluent configuration methods for Logger configuration
25+
/// Fluent configuration methods for Logger configuration
2626
/// </summary>
2727
public static class SplunkLoggingConfigurationExtensions
28-
{
28+
{
29+
internal const string DefaultOutputTemplate =
30+
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
2931

30-
internal const string DefaultOutputTemplate =
31-
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
32+
/// <summary>
33+
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
34+
/// </summary>
35+
/// <param name="configuration">The logger config</param>
36+
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
37+
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
38+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
39+
/// <param name="outputTemplate">The output template to be used when logging</param>
40+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
41+
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
42+
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
43+
/// <param name="batchSizeLimit">The size of the batch</param>
44+
/// <returns></returns>
45+
public static LoggerConfiguration EventCollector(
46+
this LoggerSinkConfiguration configuration,
47+
string splunkHost,
48+
string eventCollectorToken,
49+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
50+
string outputTemplate = DefaultOutputTemplate,
51+
IFormatProvider formatProvider = null,
52+
bool renderTemplate = true,
53+
int batchIntervalInSeconds = 2,
54+
int batchSizeLimit = 100)
55+
{
56+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
57+
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
3258

33-
/// <summary>
34-
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
35-
/// </summary>
36-
/// <param name="configuration">The logger config</param>
37-
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
38-
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
39-
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
40-
/// <param name="outputTemplate">The output template to be used when logging</param>
41-
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
42-
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
43-
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
44-
/// <param name="batchSizeLimit">The size of the batch</param>
45-
/// <returns></returns>
46-
public static LoggerConfiguration EventCollector(
47-
this LoggerSinkConfiguration configuration,
48-
string splunkHost,
49-
string eventCollectorToken,
50-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
51-
string outputTemplate = DefaultOutputTemplate,
52-
IFormatProvider formatProvider = null,
53-
bool renderTemplate = true,
54-
int batchIntervalInSeconds = 2,
55-
int batchSizeLimit = 100)
56-
{
57-
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
58-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
59+
var eventCollectorSink = new EventCollectorSink(
60+
splunkHost,
61+
eventCollectorToken,
62+
batchIntervalInSeconds,
63+
batchSizeLimit,
64+
formatProvider,
65+
renderTemplate);
5966

60-
var eventCollectorSink = new EventCollectorSink(
61-
splunkHost,
62-
eventCollectorToken,
63-
batchIntervalInSeconds,
64-
batchSizeLimit,
65-
formatProvider,
66-
renderTemplate);
67-
68-
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
69-
}
70-
71-
72-
/// <summary>
73-
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
74-
/// </summary>
75-
/// <param name="configuration">The logger config</param>
76-
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
77-
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
78-
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
79-
/// <param name="outputTemplate">The output template to be used when logging</param>
80-
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
81-
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
82-
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
83-
/// <param name="batchSizeLimit">The size of the batch</param>
84-
/// <returns></returns>
85-
public static LoggerConfiguration SplunkViaEventCollector(
86-
this LoggerSinkConfiguration configuration,
87-
string splunkHost,
88-
string eventCollectorToken,
89-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
90-
string outputTemplate = DefaultOutputTemplate,
91-
IFormatProvider formatProvider = null,
92-
bool renderTemplate = true,
93-
int batchIntervalInSeconds = 2,
94-
int batchSizeLimit = 100)
95-
{
96-
return EventCollector(configuration, splunkHost, eventCollectorToken, restrictedToMinimumLevel,
97-
outputTemplate, formatProvider, renderTemplate, batchIntervalInSeconds, batchSizeLimit);
98-
}
67+
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel);
68+
}
9969

70+
/// <summary>
71+
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
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+
/// <returns></returns>
83+
public static LoggerConfiguration SplunkViaEventCollector(
84+
this LoggerSinkConfiguration configuration,
85+
string splunkHost,
86+
string eventCollectorToken,
87+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
88+
string outputTemplate = DefaultOutputTemplate,
89+
IFormatProvider formatProvider = null,
90+
bool renderTemplate = true,
91+
int batchIntervalInSeconds = 2,
92+
int batchSizeLimit = 100)
93+
{
94+
return EventCollector(configuration, splunkHost, eventCollectorToken, restrictedToMinimumLevel,
95+
outputTemplate, formatProvider, renderTemplate, batchIntervalInSeconds, batchSizeLimit);
10096
}
97+
}
10198
}

src/sample/Program.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,14 @@ public class Program
1414
{
1515
public static void Main(string[] args)
1616
{
17-
string splunkHost = "http://192.168.71.1:8088/services/collector/event";
17+
string splunkHost = "http://192.168.71.1:8088";
1818
string splunkEventCollectorToken = "274AD921-FB85-429B-B09E-4EE069843218";
19-
20-
#if ServiceManager
21-
22-
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
23-
19+
2420
Log.Logger = new LoggerConfiguration()
2521
.WriteTo.LiterateConsole()
2622
.WriteTo.EventCollector (splunkHost, splunkEventCollectorToken)
2723
.CreateLogger();
28-
#else
29-
var handler = new System.Net.Http.WinHttpHandler();
30-
handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
31-
32-
33-
Log.Logger = new LoggerConfiguration()
34-
.WriteTo.LiterateConsole()
35-
.WriteTo.EventCollector (splunkHost, splunkEventCollectorToken, handler)
36-
.CreateLogger();
37-
38-
#endif
39-
24+
4025
Serilog.Debugging.SelfLog.Out = Console.Out;
4126

4227
Log.Information("Sample starting up");

src/sample/project.lock.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"lib/net45/Serilog.Sinks.Literate.dll": {}
5050
}
5151
},
52-
"Serilog.Sinks.Splunk/2.0.0": {
52+
"Serilog.Sinks.Splunk/2.0.0-1": {
5353
"type": "project",
5454
"framework": ".NETFramework,Version=v4.5",
5555
"dependencies": {
@@ -144,7 +144,7 @@
144144
"lib/dotnet5.1/Serilog.Sinks.Literate.dll": {}
145145
}
146146
},
147-
"Serilog.Sinks.Splunk/2.0.0": {
147+
"Serilog.Sinks.Splunk/2.0.0-1": {
148148
"type": "project",
149149
"framework": ".NETPlatform,Version=v5.4",
150150
"dependencies": {
@@ -791,7 +791,7 @@
791791
"lib/net45/Serilog.Sinks.Literate.dll": {}
792792
}
793793
},
794-
"Serilog.Sinks.Splunk/2.0.0": {
794+
"Serilog.Sinks.Splunk/2.0.0-1": {
795795
"type": "project",
796796
"framework": ".NETFramework,Version=v4.5",
797797
"dependencies": {
@@ -853,7 +853,7 @@
853853
"lib/net45/Serilog.Sinks.Literate.dll": {}
854854
}
855855
},
856-
"Serilog.Sinks.Splunk/2.0.0": {
856+
"Serilog.Sinks.Splunk/2.0.0-1": {
857857
"type": "project",
858858
"framework": ".NETFramework,Version=v4.5",
859859
"dependencies": {
@@ -1181,7 +1181,7 @@
11811181
"lib/dotnet5.1/Serilog.Sinks.Literate.dll": {}
11821182
}
11831183
},
1184-
"Serilog.Sinks.Splunk/2.0.0": {
1184+
"Serilog.Sinks.Splunk/2.0.0-1": {
11851185
"type": "project",
11861186
"framework": ".NETPlatform,Version=v5.4",
11871187
"dependencies": {
@@ -2197,7 +2197,7 @@
21972197
"lib/dotnet5.1/Serilog.Sinks.Literate.dll": {}
21982198
}
21992199
},
2200-
"Serilog.Sinks.Splunk/2.0.0": {
2200+
"Serilog.Sinks.Splunk/2.0.0-1": {
22012201
"type": "project",
22022202
"framework": ".NETPlatform,Version=v5.4",
22032203
"dependencies": {
@@ -2902,7 +2902,7 @@
29022902
}
29032903
},
29042904
"libraries": {
2905-
"Serilog.Sinks.Splunk/2.0.0": {
2905+
"Serilog.Sinks.Splunk/2.0.0-1": {
29062906
"type": "project",
29072907
"path": "../Serilog.Sinks.Splunk/project.json"
29082908
},

0 commit comments

Comments
 (0)