Skip to content

Commit b51f21e

Browse files
authored
Merge pull request #27 from nblumhardt/f-valueformatter
Update build and test infrastructure; move to standalone JSON formatter
2 parents f4e5dbf + 31d1d65 commit b51f21e

File tree

16 files changed

+336
-138
lines changed

16 files changed

+336
-138
lines changed

Build.ps1

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
1+
echo "build: Build started"
2+
13
Push-Location $PSScriptRoot
24

3-
if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse }
5+
if(Test-Path .\artifacts) {
6+
echo "build: Cleaning .\artifacts"
7+
Remove-Item .\artifacts -Force -Recurse
8+
}
49

5-
& dotnet restore
10+
& dotnet restore --no-cache
611

7-
$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
12+
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
13+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
14+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
815

9-
Push-Location src/Serilog.Sinks.Splunk
16+
echo "build: Version suffix is $suffix"
1017

11-
& dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$revision
12-
if($LASTEXITCODE -ne 0) { exit 1 }
18+
foreach ($src in ls src/*) {
19+
Push-Location $src
1320

21+
echo "build: Packaging project in $src"
22+
23+
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
24+
if($LASTEXITCODE -ne 0) { exit 1 }
25+
26+
Pop-Location
27+
}
28+
29+
foreach ($test in ls test/*.PerformanceTests) {
30+
Push-Location $test
31+
32+
echo "build: Building performance test project in $test"
33+
34+
& dotnet build -c Release
35+
if($LASTEXITCODE -ne 0) { exit 2 }
36+
37+
Pop-Location
38+
}
39+
40+
foreach ($test in ls test/*.Tests) {
41+
Push-Location $test
42+
43+
echo "build: Testing project in $test"
44+
45+
& dotnet test -c Release
46+
if($LASTEXITCODE -ne 0) { exit 3 }
47+
48+
Pop-Location
49+
}
1450

15-
Pop-Location
1651
Pop-Location

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ deploy:
1919
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
2020
skip_symbols: true
2121
on:
22-
branch: master
22+
branch: /^(master|dev)$/
2323
- provider: GitHub
2424
auth_token:
25-
secure: ggZTqqV1z0xecDoQbeoy3A7xikShCt9FWZIGp95dG9Fo0p5RAT9oGU0ZekHfUIwk
25+
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
2626
artifact: /Serilog.*\.nupkg/
2727
tag: v$(appveyor_build_version)
2828
on:

global.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{
1+
{
22
"projects": [ "src", "test" ],
33
"sdk": {
4-
"version": "1.0.0-preview1-002702"
4+
"version": "1.0.0-preview2-003121"
55
}
66
}

serilog-sinks-splunk.sln

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.24720.0
4+
VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7A774CBB-A6E9-4854-B4DB-4CF860B0C1C5}"
77
EndProject
@@ -25,6 +25,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{1C75E4
2525
EndProject
2626
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Sample", "src\sample\Sample.xproj", "{17497155-5D94-45DF-81D9-BD960E8CF217}"
2727
EndProject
28+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B9451AD8-09B9-4C09-A152-FBAE24806614}"
29+
EndProject
30+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.Splunk.Tests", "test\Serilog.Sinks.Splunk.Tests\Serilog.Sinks.Splunk.Tests.xproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
31+
EndProject
2832
Global
2933
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3034
Debug|Any CPU = Debug|Any CPU
@@ -39,12 +43,17 @@ Global
3943
{17497155-5D94-45DF-81D9-BD960E8CF217}.Debug|Any CPU.Build.0 = Debug|Any CPU
4044
{17497155-5D94-45DF-81D9-BD960E8CF217}.Release|Any CPU.ActiveCfg = Release|Any CPU
4145
{17497155-5D94-45DF-81D9-BD960E8CF217}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.Build.0 = Release|Any CPU
4250
EndGlobalSection
4351
GlobalSection(SolutionProperties) = preSolution
4452
HideSolutionNode = FALSE
4553
EndGlobalSection
4654
GlobalSection(NestedProjects) = preSolution
4755
{32CF915C-3ECD-496C-8FDB-1214581274A6} = {7A774CBB-A6E9-4854-B4DB-4CF860B0C1C5}
4856
{17497155-5D94-45DF-81D9-BD960E8CF217} = {1C75E4A9-4CB1-497C-AD17-B438882051A1}
57+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {B9451AD8-09B9-4C09-A152-FBAE24806614}
4958
EndGlobalSection
5059
EndGlobal

src/Serilog.Sinks.Splunk/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
using System.Runtime.CompilerServices;
33

44
[assembly: AssemblyVersion("2.0.0.0")]
5+
6+
[assembly: InternalsVisibleTo("Serilog.Sinks.Splunk.Tests")]

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,11 @@
1414

1515

1616
using System;
17-
using System.Globalization;
1817
using System.Net.Http;
1918
using System.Text;
2019

2120
namespace Serilog.Sinks.Splunk
2221
{
23-
internal class SplunkEvent
24-
{
25-
private string _payload;
26-
27-
internal SplunkEvent(string logEvent, string source, string sourceType, string host, string index, double time)
28-
{
29-
_payload = string.Empty;
30-
31-
var jsonPayLoad = @"{""event"":" + logEvent
32-
.Replace("\r\n", string.Empty);
33-
34-
if (!string.IsNullOrWhiteSpace(source))
35-
{
36-
jsonPayLoad = jsonPayLoad + @",""source"":""" + source + @"""";
37-
}
38-
if (!string.IsNullOrWhiteSpace(sourceType))
39-
{
40-
jsonPayLoad = jsonPayLoad + @",""sourceType"":""" + sourceType + @"""";
41-
}
42-
if (!string.IsNullOrWhiteSpace(host))
43-
{
44-
jsonPayLoad = jsonPayLoad + @",""host"":""" + host + @"""";
45-
}
46-
if (!string.IsNullOrWhiteSpace(index))
47-
{
48-
jsonPayLoad = jsonPayLoad + @",""index"":""" + index + @"""";
49-
}
50-
51-
if (time > 0)
52-
{
53-
jsonPayLoad = jsonPayLoad + @",""time"":" + time.ToString(CultureInfo.InvariantCulture);
54-
}
55-
56-
jsonPayLoad = jsonPayLoad + "}";
57-
_payload = jsonPayLoad;
58-
}
59-
60-
public string Payload
61-
{
62-
get { return _payload; }
63-
}
64-
}
65-
6622
internal class EventCollectorRequest : HttpRequestMessage
6723
{
6824
internal EventCollectorRequest(string splunkHost, string jsonPayLoad, string uri ="services/collector")

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

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using System.IO;
1919
using System.Linq;
2020
using System.Net;
21-
using System.Net.Http;
2221
using System.Threading;
2322
using System.Threading.Tasks;
2423
using Serilog.Core;
@@ -33,20 +32,11 @@ namespace Serilog.Sinks.Splunk
3332
public class EventCollectorSink : ILogEventSink, IDisposable
3433
{
3534
private readonly string _splunkHost;
36-
private readonly string _eventCollectorToken;
37-
private readonly string _source;
38-
private readonly string _sourceType;
39-
private readonly string _host;
40-
private readonly string _index;
4135
private readonly string _uriPath;
4236
private readonly int _batchSizeLimitLimit;
4337
private readonly SplunkJsonFormatter _jsonFormatter;
4438
private readonly ConcurrentQueue<LogEvent> _queue;
4539
private readonly EventCollectorClient _httpClient;
46-
private const string DefaultSource = "";
47-
private const string DefaultSourceType = "";
48-
private const string DefaultHost = "";
49-
private const string DefaultIndex = "";
5040

5141
/// <summary>
5242
/// Taken from Splunk.Logging.Common
@@ -56,7 +46,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
5646
HttpStatusCode.Forbidden,
5747
HttpStatusCode.MethodNotAllowed,
5848
HttpStatusCode.BadRequest
59-
};
49+
};
6050

6151
/// <summary>
6252
/// Creates a new instance of the sink
@@ -73,24 +63,16 @@ public EventCollectorSink(
7363
int batchIntervalInSeconds = 5,
7464
int batchSizeLimit = 100,
7565
IFormatProvider formatProvider = null,
76-
bool renderTemplate = true
77-
)
66+
bool renderTemplate = true)
67+
: this(
68+
splunkHost,
69+
eventCollectorToken,
70+
null, null, null, null, null,
71+
batchIntervalInSeconds,
72+
batchSizeLimit,
73+
formatProvider,
74+
renderTemplate)
7875
{
79-
_splunkHost = splunkHost;
80-
_eventCollectorToken = eventCollectorToken;
81-
_queue = new ConcurrentQueue<LogEvent>();
82-
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
83-
_batchSizeLimitLimit = batchSizeLimit;
84-
85-
var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);
86-
_httpClient = new EventCollectorClient(_eventCollectorToken);
87-
88-
var cancellationToken = new CancellationToken();
89-
90-
RepeatAction.OnInterval(
91-
batchInterval,
92-
async () => await ProcessQueue(),
93-
cancellationToken);
9476
}
9577

9678
/// <summary>
@@ -118,19 +100,23 @@ public EventCollectorSink(
118100
int batchIntervalInSeconds,
119101
int batchSizeLimit,
120102
IFormatProvider formatProvider = null,
121-
bool renderTemplate = true
122-
) : this(splunkHost,
123-
eventCollectorToken,
124-
batchIntervalInSeconds,
125-
batchSizeLimit,
126-
formatProvider,
127-
renderTemplate)
103+
bool renderTemplate = true)
128104
{
129-
_source = source;
130-
_sourceType = sourceType;
131-
_host = host;
132-
_index = index;
133105
_uriPath = uriPath;
106+
_splunkHost = splunkHost;
107+
_queue = new ConcurrentQueue<LogEvent>();
108+
_jsonFormatter = new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index);
109+
_batchSizeLimitLimit = batchSizeLimit;
110+
111+
var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);
112+
_httpClient = new EventCollectorClient(eventCollectorToken);
113+
114+
var cancellationToken = new CancellationToken();
115+
116+
RepeatAction.OnInterval(
117+
batchInterval,
118+
async () => await ProcessQueue(),
119+
cancellationToken);
134120
}
135121

136122
/// <summary>
@@ -175,21 +161,14 @@ private async Task ProcessQueue()
175161

176162
private async Task Send(IEnumerable<LogEvent> events)
177163
{
178-
string allEvents = string.Empty;
164+
var allEvents = new StringWriter();
179165

180166
foreach (var logEvent in events)
181167
{
182-
var sw = new StringWriter();
183-
_jsonFormatter.Format(logEvent, sw);
184-
185-
var serialisedEvent = sw.ToString();
186-
187-
var splunkEvent = new SplunkEvent(serialisedEvent, _source, _sourceType, _host, _index, logEvent.Timestamp.ToEpoch());
188-
189-
allEvents = $"{allEvents}{splunkEvent.Payload}";
168+
_jsonFormatter.Format(logEvent, allEvents);
190169
}
191170

192-
var request = new EventCollectorRequest(_splunkHost, allEvents, _uriPath);
171+
var request = new EventCollectorRequest(_splunkHost, allEvents.ToString(), _uriPath);
193172
var response = await _httpClient.SendAsync(request);
194173

195174
if (response.IsSuccessStatusCode)

0 commit comments

Comments
 (0)