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

Feat async sinks #149

Merged
merged 39 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
021a0b6
Merge pull request #24 from serilog/dev
merbla Jul 1, 2016
6ed3774
Merge pull request #35 from serilog/dev
merbla Oct 7, 2016
cf4f649
Merge pull request #41 from serilog/dev
merbla Oct 25, 2016
95baa54
Merge pull request #44 from serilog/dev
merbla Jan 28, 2017
f3c8d59
Merge pull request #46 from serilog/dev
merbla Mar 3, 2017
0dc2fb5
Merge pull request #54 from serilog/dev
merbla May 28, 2017
c22eea8
Merge pull request #57 from serilog/dev
merbla May 28, 2017
092d929
Merge pull request #61 from serilog/dev
merbla Jun 21, 2017
7ebe67e
Merge pull request #69 from serilog/dev
merbla Sep 12, 2017
977dabb
Merge pull request #70 from serilog/dev
merbla Sep 13, 2017
63e95b9
Merge pull request #81 from serilog/dev
merbla Jan 23, 2018
3a2107e
Merge pull request #103 from serilog/dev
merbla Mar 15, 2019
ade9df9
Merge pull request #108 from serilog/dev
merbla Apr 10, 2019
fe81dd4
Merge pull request #111 from serilog/dev
merbla May 15, 2019
0bc82f5
Merge pull request #115 from serilog/dev
merbla Jun 20, 2019
01af224
Merge pull request #128 from serilog/dev
merbla Jul 2, 2020
eb30e85
Added QueueLimit parameter and passing to base PeriodicBatchingSink
JuanjoFuchs Oct 9, 2020
d79a716
Merge pull request #134 from JuanjoFuchs/QueueLimitParameterAdded
merbla Oct 12, 2020
f47af1f
Merge pull request #135 from serilog/dev
merbla Oct 12, 2020
d4eda07
BoundedConcurrentQueue does not accept a negative queue limit. Using …
JuanjoFuchs Oct 13, 2020
e764e14
Merge pull request #138 from JuanjoFuchs/QueueLimitParameterAdded
merbla Oct 15, 2020
e588096
Merge pull request #140 from serilog/dev
merbla Oct 15, 2020
4bcf765
EngRajabi Oct 17, 2021
2856d1b
EngRajabi Oct 17, 2021
5637449
EngRajabi Oct 17, 2021
6d56edc
EngRajabi Oct 17, 2021
5e1f41f
EngRajabi Oct 17, 2021
359bed2
EngRajabi Oct 19, 2021
7023fd8
EngRajabi Oct 19, 2021
54cb4b6
EngRajabi Oct 19, 2021
578f321
EngRajabi Oct 19, 2021
25a8639
EngRajabi Oct 19, 2021
af8d42d
EngRajabi Oct 19, 2021
6d8dd3b
EngRajabi Oct 19, 2021
8889e11
EngRajabi Oct 19, 2021
3e67b17
EngRajabi Oct 19, 2021
c74b417
EngRajabi Oct 21, 2021
b2b7dc3
add default batch config
EngRajabi Oct 21, 2021
a73f364
EngRajabi Oct 21, 2021
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
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: '{build}'
skip_tags: true
image:
- Visual Studio 2017
- Ubuntu
- Visual Studio 2019
- Ubuntu2004
configuration:
- Release
build_script:
Expand All @@ -14,7 +14,7 @@ for:
-
matrix:
only:
- image: Ubuntu
- image: Ubuntu2004
build_script:
- sh build.sh
deploy:
Expand Down
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ dotnet restore

# Until # 65 is addressed build only core package. When available move to netstandard for all packages
for path in src/**/Serilog.Sinks.Splunk.csproj; do
dotnet build -f netstandard1.1 -c Release ${path}
dotnet build -f netstandard2.0 -c Release ${path}
dotnet build -f netstandard2.1 -c Release ${path}
done

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

dotnet build -f netcoreapp2.0 -c Release sample/Sample/Sample.csproj
dotnet build -f net5.0 -c Release sample/Sample/Sample.csproj
11 changes: 5 additions & 6 deletions sample/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="Serilog.Settings.Configuration" Version="2.6.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 6 additions & 26 deletions src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<Description>The Splunk Sink for Serilog</Description>
<VersionPrefix>3.6.0</VersionPrefix>
<VersionPrefix>3.7.0</VersionPrefix>
<Authors>Matthew Erbs, Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Sinks.Splunk</AssemblyName>
<PackageId>Serilog.Sinks.Splunk</PackageId>
<PackageTags>serilog;splunk;logging;event;collector;hec</PackageTags>
<PackageIconUrl>http://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
<icon>http://serilog.net/images/serilog-sink-nuget.png</icon>
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-splunk</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<license>http://www.apache.org/licenses/LICENSE-2.0</license>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-splunk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
Expand All @@ -20,29 +20,9 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<!-- Don't reference unused System assemblies -->
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
<!-- Don't reference the full NETStandard.Library -->
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0" />
</ItemGroup>

</Project>
28 changes: 6 additions & 22 deletions src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,25 @@

<PropertyGroup>
<Description>The Splunk TCP Sink for Serilog</Description>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>
<Authors>Matthew Erbs, Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Sinks.Splunk.TCP</AssemblyName>
<PackageId>Serilog.Sinks.Splunk.TCP</PackageId>
<PackageTags>serilog;splunk;logging;tcp</PackageTags>
<PackageIconUrl>http://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
<icon>http://serilog.net/images/serilog-sink-nuget.png</icon>
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-splunk</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<license>http://www.apache.org/licenses/LICENSE-2.0</license>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-splunk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<!-- Don't reference unused System assemblies -->
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<PackageReference Include="Splunk.Logging.Common" Version="1.7.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<!--<PackageReference Include="System.Net.Http" Version="4.3.0" />-->
<PackageReference Include="Splunk.Logging.Common.Core" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.6.0" />
<PackageReference Include="Splunk.Logging.Common.Core" Version="1.0.0" />
</ItemGroup>
</Project>
25 changes: 5 additions & 20 deletions src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,24 @@

<PropertyGroup>
<Description>The Splunk UDP Sink for Serilog</Description>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>
<Authors>Matthew Erbs, Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Sinks.Splunk.UDP</AssemblyName>
<PackageId>Serilog.Sinks.Splunk.UDP</PackageId>
<PackageTags>serilog;splunk;logging;udp</PackageTags>
<PackageIconUrl>http://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
<icon>http://serilog.net/images/serilog-sink-nuget.png</icon>
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-splunk</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<license>http://www.apache.org/licenses/LICENSE-2.0</license>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-splunk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<!-- Don't reference unused System assemblies -->
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.6.0" />
</ItemGroup>
</Project>
30 changes: 30 additions & 0 deletions src/Serilog.Sinks.UDP/Sinks/Splunk/SplunkUdpSinkConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Net;

namespace Serilog.Sinks.Splunk
Expand All @@ -22,6 +23,8 @@ namespace Serilog.Sinks.Splunk
/// </summary>
public class SplunkUdpSinkConnectionInfo
{
private int _queueSizeLimit;

/// <summary>
/// Splunk host.
/// </summary>
Expand All @@ -32,6 +35,33 @@ public class SplunkUdpSinkConnectionInfo
/// </summary>
public int Port { get; }

///<summary>
/// The maximum number of events to post in a single batch. Defaults to: 50.
/// </summary>
public int BatchPostingLimit { get; set; }


///<summary>
/// The time to wait between checking for event batches. Defaults to 2 seconds.
/// </summary>
public TimeSpan Period { get; set; }


/// <summary>
/// The maximum number of events that will be held in-memory while waiting to ship them to
/// Elasticsearch. Beyond this limit, events will be dropped. The default is 100,000. Has no effect on
EngRajabi marked this conversation as resolved.
Show resolved Hide resolved
/// durable log shipping.
/// </summary>
public int QueueSizeLimit
{
get { return _queueSizeLimit; }
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(QueueSizeLimit), "Queue size limit must be non-zero.");
_queueSizeLimit = value;
}
}
/// <summary>
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
/// for defining connection info for connecting using UDP against Splunk.
Expand Down
88 changes: 46 additions & 42 deletions src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@
using Serilog.Events;
using Serilog.Formatting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog.Sinks.Splunk
{
/// <summary>
/// A sink that logs to Splunk over UDP
/// </summary>
public class UdpSink : ILogEventSink, IDisposable
public class UdpSink : PeriodicBatchingSink
{
private readonly SplunkUdpSinkConnectionInfo _connectionInfo;
private readonly ITextFormatter _formatter;
private Socket _socket;
private bool disposedValue = false;

/// <summary>
/// Creates an instance of the Splunk UDP Sink.
Expand All @@ -48,51 +50,15 @@ public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, IFormatProvider forma
/// </summary>
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, ITextFormatter formatter)
public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, ITextFormatter formatter)
: base(connectionInfo.BatchPostingLimit, connectionInfo.Period, connectionInfo.QueueSizeLimit)
{
_connectionInfo = connectionInfo;
_formatter = formatter;
Connect();
}

/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
DisposeSocket();
}

disposedValue = true;
}
}

/// <inheritdoc/>
public void Dispose()
{
Dispose(true);
}

/// <inheritdoc/>
public void Emit(LogEvent logEvent)
{
byte[] data = Convert(logEvent);

try
{
_socket.Send(data);
}
catch (SocketException)
{
// Try to reconnect and log
DisposeSocket();
Connect();
_socket.Send(data);
}
}



private byte[] Convert(LogEvent logEvent)
{
var sb = new StringBuilder();
Expand All @@ -107,13 +73,51 @@ private void Connect()
_socket.Connect(_connectionInfo.Host, _connectionInfo.Port);
}


protected override void Dispose(bool disposing)
{
DisposeSocket();

base.Dispose();
}

private void DisposeSocket()
{
_socket?.Close();
_socket?.Dispose();
_socket = null;
}

/// <summary>
/// Emit a batch of log events, running to completion synchronously.
/// </summary>
/// <param name="events">The events to emit.</param>
/// <remarks>
/// Override either <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
/// or <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />,
/// not both.
/// </remarks>
protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
{
foreach (var logEvent in events)
{
byte[] data = Convert(logEvent);

try
{
_socket.Send(data);
}
catch (SocketException)
{
// Try to reconnect and log
DisposeSocket();
Connect();
_socket.Send(data);
}
}
}


private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
{
return new SplunkJsonFormatter(renderTemplate, formatProvider);
Expand Down
Loading