Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to serilog periodic batching 4.x #168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bin\
obj\
bin/
obj/
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM microsoft/dotnet:2.1-sdk AS build
ADD . /
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY . /
WORKDIR /sample/Sample
RUN dotnet restore
RUN dotnet publish -c Release -o out -f netcoreapp2.0
RUN dotnet publish -c Release -o out -f net6.0

FROM microsoft/dotnet:2.1-runtime AS runtime
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime
WORKDIR /sample/Sample
COPY --from=build /sample/Sample/out ./
ENTRYPOINT ["dotnet", "Sample.dll"]
5 changes: 3 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ dotnet restore
for path in src/**/Serilog.Sinks.Splunk.csproj; do
dotnet build -f netstandard2.0 -c Release ${path}
dotnet build -f netstandard2.1 -c Release ${path}
dotnet build -f net6.0 -c Release ${path}
done

for path in test/*.Tests/*.csproj; do
dotnet test -f net5.0 -c Release ${path}
dotnet test -f net6.0 -c Release ${path}
done

dotnet build -f net5.0 -c Release sample/Sample/Sample.csproj
dotnet build -f net6.0 -c Release sample/Sample/Sample.csproj
12 changes: 5 additions & 7 deletions sample/Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>7e06bf41-4a1d-4a41-bcf3-cacd41388d5f</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion sample/splunk/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FROM splunk/splunk:9.1
FROM splunk/splunk:latest
ADD etc ${SPLUNK_HOME}/etc
13 changes: 7 additions & 6 deletions src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>The Splunk Sink for Serilog</Description>
<VersionPrefix>3.7.0</VersionPrefix>
<VersionPrefix>4.0.0</VersionPrefix>
<Authors>Matthew Erbs, Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0;net6.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Sinks.Splunk</AssemblyName>
<PackageId>Serilog.Sinks.Splunk</PackageId>
Expand All @@ -18,12 +18,13 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<SignAssembly>true</SignAssembly>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>latest</LangVersion>
<RootNamespace>Serilog</RootNamespace>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="4.0.0" />
</ItemGroup>

</Project>
52 changes: 8 additions & 44 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace Serilog.Sinks.Splunk
/// <summary>
/// A sink to log to the Event Collector available in Splunk 6.3
/// </summary>
public class EventCollectorSink : PeriodicBatchingSink
public class EventCollectorSink : IBatchedLogEventSink
{
private const int DefaultQueueLimit = 100000;
internal const int DefaultQueueLimit = 100000;

private readonly string _splunkHost;
private readonly string _uriPath;
Expand All @@ -54,26 +54,17 @@ public class EventCollectorSink : PeriodicBatchingSink
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
int batchIntervalInSeconds = 5,
int batchSizeLimit = 100,
int? queueLimit = null,
IFormatProvider formatProvider = null,
bool renderTemplate = true)
: this(
splunkHost,
eventCollectorToken,
null, null, null, null, null,
batchIntervalInSeconds,
batchSizeLimit,
queueLimit,
formatProvider,
renderTemplate)
{
Expand All @@ -85,11 +76,8 @@ public EventCollectorSink(
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="source">The source of the event</param>
/// <param name="sourceType">The source type of the event</param>
Expand All @@ -103,19 +91,14 @@ public EventCollectorSink(
string sourceType,
string host,
string index,
int batchIntervalInSeconds,
int batchSizeLimit,
int? queueLimit,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
HttpMessageHandler messageHandler = null)
: this(
splunkHost,
eventCollectorToken,
uriPath,
batchIntervalInSeconds,
batchSizeLimit,
queueLimit,

new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index),
messageHandler)
{
Expand All @@ -127,11 +110,8 @@ public EventCollectorSink(
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="fields">Add extra CustomExtraFields for Splunk to index</param>
/// <param name="source">The source of the event</param>
Expand All @@ -147,9 +127,6 @@ public EventCollectorSink(
string host,
string index,
CustomFields fields,
int batchIntervalInSeconds,
int batchSizeLimit,
int? queueLimit,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
HttpMessageHandler messageHandler = null)
Expand All @@ -158,9 +135,6 @@ public EventCollectorSink(
splunkHost,
eventCollectorToken,
uriPath,
batchIntervalInSeconds,
batchSizeLimit,
queueLimit,
new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index, fields),
messageHandler)
{
Expand All @@ -172,21 +146,14 @@ public EventCollectorSink(
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
int batchIntervalInSeconds,
int batchSizeLimit,
int? queueLimit,
ITextFormatter jsonFormatter,
HttpMessageHandler messageHandler = null)
: base(batchSizeLimit, TimeSpan.FromSeconds(batchIntervalInSeconds), queueLimit ?? DefaultQueueLimit)
{
_uriPath = uriPath;
_splunkHost = splunkHost;
Expand All @@ -197,14 +164,8 @@ public EventCollectorSink(
: new EventCollectorClient(eventCollectorToken);
}

/// <summary>
/// Emit a batch of log events, running asynchronously.
/// </summary>
/// <param name="events">The events to emit.</param>
/// <remarks>
/// Override either <see cref="PeriodicBatchingSink.EmitBatch" /> or <see cref="PeriodicBatchingSink.EmitBatchAsync" />, not both.
/// </remarks>
protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
/// <inheritdoc />
public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
{
var allEvents = new StringWriter();

Expand Down Expand Up @@ -233,5 +194,8 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
}
}
}

/// <inheritdoc />
public Task OnEmptyBatchAsync() => Task.CompletedTask;
}
}
37 changes: 25 additions & 12 deletions src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;
using Serilog.Sinks.Splunk;

namespace Serilog
Expand Down Expand Up @@ -76,14 +77,12 @@ public static LoggerConfiguration EventCollector(
sourceType,
host,
index,
batchIntervalInSeconds,
batchSizeLimit,
queueLimit,
formatProvider,
renderTemplate,
messageHandler);

return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch);
return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch,
batchIntervalInSeconds, batchSizeLimit, queueLimit);
}

/// <summary>
Expand Down Expand Up @@ -122,13 +121,12 @@ public static LoggerConfiguration EventCollector(
splunkHost,
eventCollectorToken,
uriPath,
batchIntervalInSeconds,
batchSizeLimit,
queueLimit,

jsonFormatter,
messageHandler);

return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch);
return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch,
batchIntervalInSeconds, batchSizeLimit, queueLimit);
}


Expand Down Expand Up @@ -183,15 +181,30 @@ public static LoggerConfiguration EventCollector(
host,
index,
fields,
batchIntervalInSeconds,
batchSizeLimit,
queueLimit,
formatProvider,
renderTemplate,
messageHandler
);

return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch);
return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch,
batchIntervalInSeconds, batchSizeLimit, queueLimit);
}

private static LoggerConfiguration BuildPeriodicBatchingSink(this LoggerSinkConfiguration configuration,
EventCollectorSink eventCollectorSink,
LogEventLevel restrictedToMinimumLevel,
LoggingLevelSwitch levelSwitch = null,
int batchIntervalInSeconds = 2,
int batchSizeLimit = 100,
int? queueLimit = EventCollectorSink.DefaultQueueLimit)
{
var periodicBatchingOptions = new PeriodicBatchingSinkOptions
{
Period = TimeSpan.FromSeconds(batchIntervalInSeconds), QueueLimit = queueLimit, BatchSizeLimit = batchSizeLimit
};
var periodicBatchSink = new PeriodicBatchingSink(eventCollectorSink, periodicBatchingOptions);

return configuration.Sink(periodicBatchSink, restrictedToMinimumLevel, levelSwitch);
}
}
}