Skip to content

Commit 2cfa29b

Browse files
jpfifercvna-justinEEParker
authored
Update to serilog periodic batching 4.x (#168)
* Updated dependencies to Serilog 3.1 and Periodic Batching Sink to 4.0.0 * Update version to 4.0.0 due to the breaking constructor changes on the EventCollectorSink * Update Dockerfile with net6.0 versions * Use the latest version of Splunk The 7.2 label does not appear to be available anymore. * Add in missing build for net6.0 * Use forward slash for ignore This should work on Windows as well * Use the same Framework reference for the sample Everything else is using at least net6.0 so the Sample should as well --------- Co-authored-by: Justin Pfifer <[email protected]> Co-authored-by: Jeff Parker, PE <[email protected]>
1 parent 032f366 commit 2cfa29b

File tree

8 files changed

+55
-78
lines changed

8 files changed

+55
-78
lines changed

.dockerignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
bin\
2-
obj\
1+
bin/
2+
obj/

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM microsoft/dotnet:2.1-sdk AS build
2-
ADD . /
1+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
2+
COPY . /
33
WORKDIR /sample/Sample
44
RUN dotnet restore
5-
RUN dotnet publish -c Release -o out -f netcoreapp2.0
5+
RUN dotnet publish -c Release -o out -f net6.0
66

7-
FROM microsoft/dotnet:2.1-runtime AS runtime
7+
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime
88
WORKDIR /sample/Sample
99
COPY --from=build /sample/Sample/out ./
1010
ENTRYPOINT ["dotnet", "Sample.dll"]

build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ dotnet restore
77
for path in src/**/Serilog.Sinks.Splunk.csproj; do
88
dotnet build -f netstandard2.0 -c Release ${path}
99
dotnet build -f netstandard2.1 -c Release ${path}
10+
dotnet build -f net6.0 -c Release ${path}
1011
done
1112

1213
for path in test/*.Tests/*.csproj; do
13-
dotnet test -f net5.0 -c Release ${path}
14+
dotnet test -f net6.0 -c Release ${path}
1415
done
1516

16-
dotnet build -f net5.0 -c Release sample/Sample/Sample.csproj
17+
dotnet build -f net6.0 -c Release sample/Sample/Sample.csproj

sample/Sample/Sample.csproj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6-
<UserSecretsId>7e06bf41-4a1d-4a41-bcf3-cacd41388d5f</UserSecretsId>
76
</PropertyGroup>
87

98
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
11-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
12-
<PackageReference Include="Serilog" Version="3.0.1" />
13-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
14-
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
9+
<PackageReference Include="Serilog" Version="3.1.1" />
10+
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
11+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
1513
</ItemGroup>
1614

1715
<ItemGroup>

sample/splunk/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
FROM splunk/splunk:9.1
1+
FROM splunk/splunk:latest
22
ADD etc ${SPLUNK_HOME}/etc
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>The Splunk Sink for Serilog</Description>
5-
<VersionPrefix>3.7.0</VersionPrefix>
5+
<VersionPrefix>4.0.0</VersionPrefix>
66
<Authors>Matthew Erbs, Serilog Contributors</Authors>
7-
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
7+
<TargetFrameworks>netstandard2.1;netstandard2.0;net6.0</TargetFrameworks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<AssemblyName>Serilog.Sinks.Splunk</AssemblyName>
1010
<PackageId>Serilog.Sinks.Splunk</PackageId>
@@ -18,12 +18,13 @@
1818
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1919
<SignAssembly>true</SignAssembly>
2020
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
21-
<LangVersion>latest</LangVersion>
21+
<RootNamespace>Serilog</RootNamespace>
22+
<LangVersion>latest</LangVersion>
2223
</PropertyGroup>
2324

2425
<ItemGroup>
25-
<PackageReference Include="Serilog" Version="2.10.0" />
26-
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0" />
26+
<PackageReference Include="Serilog" Version="3.1.1" />
27+
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="4.0.0" />
2728
</ItemGroup>
2829

2930
</Project>

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

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ namespace Serilog.Sinks.Splunk
2929
/// <summary>
3030
/// A sink to log to the Event Collector available in Splunk 6.3
3131
/// </summary>
32-
public class EventCollectorSink : PeriodicBatchingSink
32+
public class EventCollectorSink : IBatchedLogEventSink
3333
{
34-
private const int DefaultQueueLimit = 100000;
34+
internal const int DefaultQueueLimit = 100000;
3535

3636
private readonly string _splunkHost;
3737
private readonly string _uriPath;
@@ -54,26 +54,17 @@ public class EventCollectorSink : PeriodicBatchingSink
5454
/// </summary>
5555
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
5656
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
57-
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
5857
/// <param name="formatProvider">The format provider used when rendering the message</param>
5958
/// <param name="renderTemplate">Whether to render the message template</param>
60-
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
61-
/// <param name="queueLimit">Maximum number of events in the queue</param>
6259
public EventCollectorSink(
6360
string splunkHost,
6461
string eventCollectorToken,
65-
int batchIntervalInSeconds = 5,
66-
int batchSizeLimit = 100,
67-
int? queueLimit = null,
6862
IFormatProvider formatProvider = null,
6963
bool renderTemplate = true)
7064
: this(
7165
splunkHost,
7266
eventCollectorToken,
7367
null, null, null, null, null,
74-
batchIntervalInSeconds,
75-
batchSizeLimit,
76-
queueLimit,
7768
formatProvider,
7869
renderTemplate)
7970
{
@@ -85,11 +76,8 @@ public EventCollectorSink(
8576
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
8677
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
8778
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
88-
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
8979
/// <param name="formatProvider">The format provider used when rendering the message</param>
9080
/// <param name="renderTemplate">Whether to render the message template</param>
91-
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
92-
/// <param name="queueLimit">Maximum number of events in the queue</param>
9381
/// <param name="index">The Splunk index to log to</param>
9482
/// <param name="source">The source of the event</param>
9583
/// <param name="sourceType">The source type of the event</param>
@@ -103,19 +91,14 @@ public EventCollectorSink(
10391
string sourceType,
10492
string host,
10593
string index,
106-
int batchIntervalInSeconds,
107-
int batchSizeLimit,
108-
int? queueLimit,
10994
IFormatProvider formatProvider = null,
11095
bool renderTemplate = true,
11196
HttpMessageHandler messageHandler = null)
11297
: this(
11398
splunkHost,
11499
eventCollectorToken,
115100
uriPath,
116-
batchIntervalInSeconds,
117-
batchSizeLimit,
118-
queueLimit,
101+
119102
new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index),
120103
messageHandler)
121104
{
@@ -127,11 +110,8 @@ public EventCollectorSink(
127110
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
128111
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
129112
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
130-
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
131-
/// <param name="queueLimit">Maximum number of events in the queue</param>
132113
/// <param name="formatProvider">The format provider used when rendering the message</param>
133114
/// <param name="renderTemplate">Whether to render the message template</param>
134-
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
135115
/// <param name="index">The Splunk index to log to</param>
136116
/// <param name="fields">Add extra CustomExtraFields for Splunk to index</param>
137117
/// <param name="source">The source of the event</param>
@@ -147,9 +127,6 @@ public EventCollectorSink(
147127
string host,
148128
string index,
149129
CustomFields fields,
150-
int batchIntervalInSeconds,
151-
int batchSizeLimit,
152-
int? queueLimit,
153130
IFormatProvider formatProvider = null,
154131
bool renderTemplate = true,
155132
HttpMessageHandler messageHandler = null)
@@ -158,9 +135,6 @@ public EventCollectorSink(
158135
splunkHost,
159136
eventCollectorToken,
160137
uriPath,
161-
batchIntervalInSeconds,
162-
batchSizeLimit,
163-
queueLimit,
164138
new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index, fields),
165139
messageHandler)
166140
{
@@ -172,21 +146,14 @@ public EventCollectorSink(
172146
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
173147
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
174148
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
175-
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
176-
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
177-
/// <param name="queueLimit">Maximum number of events in the queue</param>
178149
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
179150
/// <param name="messageHandler">The handler used to send HTTP requests</param>
180151
public EventCollectorSink(
181152
string splunkHost,
182153
string eventCollectorToken,
183154
string uriPath,
184-
int batchIntervalInSeconds,
185-
int batchSizeLimit,
186-
int? queueLimit,
187155
ITextFormatter jsonFormatter,
188156
HttpMessageHandler messageHandler = null)
189-
: base(batchSizeLimit, TimeSpan.FromSeconds(batchIntervalInSeconds), queueLimit ?? DefaultQueueLimit)
190157
{
191158
_uriPath = uriPath;
192159
_splunkHost = splunkHost;
@@ -197,14 +164,8 @@ public EventCollectorSink(
197164
: new EventCollectorClient(eventCollectorToken);
198165
}
199166

200-
/// <summary>
201-
/// Emit a batch of log events, running asynchronously.
202-
/// </summary>
203-
/// <param name="events">The events to emit.</param>
204-
/// <remarks>
205-
/// Override either <see cref="PeriodicBatchingSink.EmitBatch" /> or <see cref="PeriodicBatchingSink.EmitBatchAsync" />, not both.
206-
/// </remarks>
207-
protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
167+
/// <inheritdoc />
168+
public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
208169
{
209170
var allEvents = new StringWriter();
210171

@@ -233,5 +194,8 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
233194
}
234195
}
235196
}
197+
198+
/// <inheritdoc />
199+
public Task OnEmptyBatchAsync() => Task.CompletedTask;
236200
}
237201
}

src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Serilog.Core;
2020
using Serilog.Events;
2121
using Serilog.Formatting;
22+
using Serilog.Sinks.PeriodicBatching;
2223
using Serilog.Sinks.Splunk;
2324

2425
namespace Serilog
@@ -76,14 +77,12 @@ public static LoggerConfiguration EventCollector(
7677
sourceType,
7778
host,
7879
index,
79-
batchIntervalInSeconds,
80-
batchSizeLimit,
81-
queueLimit,
8280
formatProvider,
8381
renderTemplate,
8482
messageHandler);
8583

86-
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch);
84+
return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch,
85+
batchIntervalInSeconds, batchSizeLimit, queueLimit);
8786
}
8887

8988
/// <summary>
@@ -122,13 +121,12 @@ public static LoggerConfiguration EventCollector(
122121
splunkHost,
123122
eventCollectorToken,
124123
uriPath,
125-
batchIntervalInSeconds,
126-
batchSizeLimit,
127-
queueLimit,
124+
128125
jsonFormatter,
129126
messageHandler);
130127

131-
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch);
128+
return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch,
129+
batchIntervalInSeconds, batchSizeLimit, queueLimit);
132130
}
133131

134132

@@ -183,15 +181,30 @@ public static LoggerConfiguration EventCollector(
183181
host,
184182
index,
185183
fields,
186-
batchIntervalInSeconds,
187-
batchSizeLimit,
188-
queueLimit,
189184
formatProvider,
190185
renderTemplate,
191186
messageHandler
192187
);
193188

194-
return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch);
189+
return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch,
190+
batchIntervalInSeconds, batchSizeLimit, queueLimit);
191+
}
192+
193+
private static LoggerConfiguration BuildPeriodicBatchingSink(this LoggerSinkConfiguration configuration,
194+
EventCollectorSink eventCollectorSink,
195+
LogEventLevel restrictedToMinimumLevel,
196+
LoggingLevelSwitch levelSwitch = null,
197+
int batchIntervalInSeconds = 2,
198+
int batchSizeLimit = 100,
199+
int? queueLimit = EventCollectorSink.DefaultQueueLimit)
200+
{
201+
var periodicBatchingOptions = new PeriodicBatchingSinkOptions
202+
{
203+
Period = TimeSpan.FromSeconds(batchIntervalInSeconds), QueueLimit = queueLimit, BatchSizeLimit = batchSizeLimit
204+
};
205+
var periodicBatchSink = new PeriodicBatchingSink(eventCollectorSink, periodicBatchingOptions);
206+
207+
return configuration.Sink(periodicBatchSink, restrictedToMinimumLevel, levelSwitch);
195208
}
196209
}
197210
}

0 commit comments

Comments
 (0)