Skip to content

Commit be9ae5f

Browse files
committed
Updated packages to latest Serilog
Also, removed obsolete code and package references
1 parent e86e99f commit be9ae5f

9 files changed

+83
-191
lines changed

src/Serilog.Sinks.Splunk.FullNetFx/LoggerConfigurationSplunkExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
using Serilog.Configuration;
1818
using Serilog.Events;
1919
using Serilog.Formatting.Display;
20-
using Serilog.Sinks.Splunk;
21-
using Splunk.Client;
20+
using Serilog.Sinks.Splunk;
2221

2322
namespace Serilog
2423
{

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@
4040
<AssemblyOriginatorKeyFile>..\..\assets\Serilog.snk</AssemblyOriginatorKeyFile>
4141
</PropertyGroup>
4242
<ItemGroup>
43+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
44+
<HintPath>..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
45+
<Private>True</Private>
46+
</Reference>
4347
<Reference Include="Serilog, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
44-
<SpecificVersion>False</SpecificVersion>
45-
<HintPath>..\..\packages\Serilog.1.5.5\lib\net45\Serilog.dll</HintPath>
48+
<HintPath>..\..\packages\Serilog.1.5.11\lib\net45\Serilog.dll</HintPath>
49+
<Private>True</Private>
4650
</Reference>
4751
<Reference Include="Serilog.FullNetFx, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
48-
<SpecificVersion>False</SpecificVersion>
49-
<HintPath>..\..\packages\Serilog.1.5.5\lib\net45\Serilog.FullNetFx.dll</HintPath>
50-
</Reference>
51-
<Reference Include="Splunk.Client, Version=2.1.2.0, Culture=neutral, processorArchitecture=MSIL">
52-
<SpecificVersion>False</SpecificVersion>
53-
<HintPath>..\..\packages\Splunk.Client.2.1.2\lib\portable-net45+win+wpa81\Splunk.Client.dll</HintPath>
52+
<HintPath>..\..\packages\Serilog.1.5.11\lib\net45\Serilog.FullNetFx.dll</HintPath>
53+
<Private>True</Private>
5454
</Reference>
55-
<Reference Include="Splunk.Logging.Common, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
56-
<SpecificVersion>False</SpecificVersion>
57-
<HintPath>..\..\packages\Splunk.Logging.Common.1.1.0\lib\net45\Splunk.Logging.Common.dll</HintPath>
55+
<Reference Include="Splunk.Logging.Common, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
56+
<HintPath>..\..\packages\Splunk.Logging.Common.1.5.0\lib\net45\Splunk.Logging.Common.dll</HintPath>
57+
<Private>True</Private>
5858
</Reference>
5959
<Reference Include="System" />
6060
<Reference Include="System.Core" />
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Serilog" version="1.5.5" targetFramework="net45" />
4-
<package id="Splunk.Client" version="2.1.2" targetFramework="net45" />
5-
<package id="Splunk.Logging.Common" version="1.1.0" targetFramework="net45" />
3+
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
4+
<package id="Serilog" version="1.5.11" targetFramework="net45" />
5+
<package id="Splunk.Logging.Common" version="1.5.0" targetFramework="net45" />
66
</packages>

src/Serilog.Sinks.Splunk/LoggerConfigurationSplunkExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Serilog.Events;
1818
using Serilog.Formatting.Display;
1919
using Serilog.Sinks.Splunk;
20-
using Splunk.Client;
2120

2221
namespace Serilog
2322
{

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,12 @@
5656
</Compile>
5757
<Compile Include="Sinks\Splunk\RepeatAction.cs" />
5858
<Compile Include="Sinks\Splunk\SplunkJsonFormatter.cs" />
59-
<Compile Include="Sinks\Splunk\SplunkLogging.cs" />
6059
<Compile Include="Sinks\Splunk\EventCollectorExtensions.cs" />
6160
<Compile Include="Sinks\Splunk\SplunkViaEventCollectorSink.cs" />
6261
</ItemGroup>
6362
<ItemGroup>
64-
<Reference Include="Serilog">
65-
<HintPath>..\..\packages\Serilog.1.5.5\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10\Serilog.dll</HintPath>
66-
</Reference>
67-
<Reference Include="Splunk.Client">
68-
<HintPath>..\..\packages\Splunk.Client.2.1.2\lib\portable-net45+win+wpa81\Splunk.Client.dll</HintPath>
63+
<Reference Include="Serilog, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
64+
<HintPath>..\..\packages\Serilog.1.5.11\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10\Serilog.dll</HintPath>
6965
<Private>True</Private>
7066
</Reference>
7167
</ItemGroup>

src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.nuspec

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
<tags>serilog logging splunk</tags>
1313
<dependencies>
1414
<group>
15-
<dependency id="Serilog" version="[1.5,2)" />
16-
<dependency id="Splunk.Client" version="[2.1,3]" />
15+
<dependency id="Serilog" version="[1.5.11,2)" />
1716
</group>
1817
<group targetFramework="net45">
19-
<dependency id="Serilog" version="[1.5,2)" />
20-
<dependency id="Splunk.Client" version="[2.1,3]" />
21-
<dependency id="Splunk.Logging.Common" version="[1.1,2]" />
18+
<dependency id="Serilog" version="[1.5.11,2)" />
19+
<dependency id="Splunk.Logging.Common" version="[1.5.0,2]" />
2220
</group>
2321
</dependencies>
2422
</metadata>

src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkLogging.cs

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkViaEventCollectorSink.cs

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Linq;
6+
using System.Net;
57
using System.Net.Http;
68
using System.Net.Http.Headers;
79
using System.Text;
@@ -10,27 +12,51 @@
1012
using Serilog.Core;
1113
using Serilog.Debugging;
1214
using Serilog.Events;
13-
using Serilog.Formatting.Json;
1415

1516
namespace Serilog.Sinks.Splunk
1617
{
18+
internal class EventCollectorClient : HttpClient
19+
{
20+
public EventCollectorClient(string eventCollectorToken) : base()
21+
{
22+
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", eventCollectorToken);
23+
}
24+
}
25+
26+
internal class EventCollectorRequest : HttpRequestMessage
27+
{
28+
public EventCollectorRequest(string splunkHost, string logEvent)
29+
{
30+
var jsonPayLoad = @"{""event"":" + logEvent + "}"
31+
.Replace("\r\n", string.Empty);
32+
33+
var stringContent = new StringContent(jsonPayLoad, Encoding.UTF8, "application/json");
34+
RequestUri = new Uri(splunkHost);
35+
Content = stringContent;
36+
Method = HttpMethod.Post;
37+
}
38+
}
39+
40+
41+
1742
/// <summary>
1843
/// A sink to log to the Event Collector available in Splunk 6.3
1944
/// </summary>
2045
public class SplunkViaEventCollectorSink : ILogEventSink
2146
{
22-
private string _splunkHost;
23-
private string _eventCollectorToken;
24-
private int _batchSizeLimitLimit;
25-
private JsonFormatter _jsonFormatter;
26-
private ConcurrentQueue<LogEvent> _queue;
27-
private TimeSpan _batchInterval;
28-
29-
30-
//public const int DefaultBatchInterval = 10 * 1000; // 10 seconds
31-
//public const int DefaultBatchSize = 10 * 1024; // 10KB
32-
//public const int DefaultBatchCount = 10;
33-
47+
private readonly string _splunkHost;
48+
private readonly string _eventCollectorToken;
49+
private readonly int _batchSizeLimitLimit;
50+
private readonly SplunkJsonFormatter _jsonFormatter;
51+
private readonly ConcurrentQueue<LogEvent> _queue;
52+
private readonly TimeSpan _batchInterval;
53+
54+
private static readonly HttpStatusCode[] HttpEventCollectorApplicationErrors =
55+
{
56+
HttpStatusCode.Forbidden,
57+
HttpStatusCode.MethodNotAllowed,
58+
HttpStatusCode.BadRequest
59+
};
3460

3561
/// <summary>
3662
/// Creates a new instance of the sink
@@ -53,7 +79,6 @@ public SplunkViaEventCollectorSink(
5379
_splunkHost = splunkHost;
5480
_eventCollectorToken = eventCollectorToken;
5581
_queue = new ConcurrentQueue<LogEvent>();
56-
5782
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate:renderTemplate);
5883
_batchSizeLimitLimit = batchSizeLimit;
5984
_batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);
@@ -91,53 +116,39 @@ private async Task ProcessQueue()
91116
if (events.Count == 0)
92117
return;
93118

94-
var sw = new StringWriter();
95-
96-
//TODO: Check status code against defaults
97-
//TODO: Put items back in queue if matching use case
98-
//TODO: Change to use retry methods
99-
100-
101-
foreach (var logEvent in events)
102-
{
103-
_jsonFormatter.Format(logEvent, sw);
104-
105-
var logEventAsAString = sw.ToString();
119+
//TODO: Add streaming capability for performance
106120

107-
108-
var plainPayload2 = @"{""event"":" + logEventAsAString + "}";
109-
110-
111-
using (var client = new HttpClient())
121+
using (var client = new EventCollectorClient(_eventCollectorToken))
122+
{
123+
foreach (var logEvent in events)
112124
{
125+
var sw = new StringWriter();
113126

114-
115-
var stringContent = new StringContent(plainPayload2, Encoding.UTF8, "application/json");
116-
117-
plainPayload2 = plainPayload2.Replace("\r\n", string.Empty);
118-
119-
120-
121-
var request = new HttpRequestMessage
122-
{
123-
RequestUri = new Uri(_splunkHost),
124-
Content = stringContent
125-
};
126-
127-
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", _eventCollectorToken);
128-
129-
request.Method = HttpMethod.Post;
130-
127+
_jsonFormatter.Format(logEvent, sw);
128+
var le = sw.ToString();
129+
var request = new EventCollectorRequest(_splunkHost, le);
131130
var response = await client.SendAsync(request);
132131

133-
if (response.IsSuccessStatusCode)
134-
{
132+
if (response.IsSuccessStatusCode) {
133+
//Do Nothing?
135134
}
136135
else
137136
{
137+
//Application Errors sent via HTTP Event Collector
138+
if (HttpEventCollectorApplicationErrors.Any(x => x == response.StatusCode))
139+
{
140+
SelfLog.WriteLine("A status code of {0} was received when attempting to send to {1}. The event has been discarded", response.StatusCode.ToString(), _splunkHost);
141+
}
142+
else
143+
{
144+
//Put the item back in the queue & retry on next go
145+
SelfLog.WriteLine("A status code of {0} was received when attempting to send to {1}. The event has been placed back in the queue", response.StatusCode.ToString(), _splunkHost);
146+
147+
_queue.Enqueue(logEvent);
148+
}
138149
}
139150
}
140-
}
151+
}
141152
} while (true);
142153
}
143154
catch (Exception ex)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Serilog" version="1.5.5" targetFramework="portable-net45+win" />
4-
<package id="Splunk.Client" version="2.1.2" targetFramework="portable-net45+win" />
3+
<package id="Serilog" version="1.5.11" targetFramework="portable45-net45+win8" />
54
</packages>

0 commit comments

Comments
 (0)