Skip to content

Commit

Permalink
Refactoring and delete Newtonsoft.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Волков Антон Александрович committed Jun 6, 2023
1 parent 7810203 commit 9e99541
Show file tree
Hide file tree
Showing 32 changed files with 398 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static class LoggerConfigurationGrayLogExtensions
options.MaxMessageSizeInUdp = maxMessageSizeInUdp;
options.IncludeMessageTemplate = includeMessageTemplate;
options.MessageTemplateFieldName = messageTemplateFieldName;

options.TransportType = TransportType.Udp;
return loggerSinkConfiguration.Graylog(options);
}
}
Expand Down
56 changes: 10 additions & 46 deletions src/Serilog.Sinks.Graylog.Batching/PeriodicBatchingGraylogSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Sinks.Graylog.Core;
Expand All @@ -11,62 +10,33 @@

namespace Serilog.Sinks.Graylog.Batching
{
public class PeriodicBatchingGraylogSink : PeriodicBatchingSink
using System.Text.Json.Nodes;

public class PeriodicBatchingGraylogSink : IBatchedLogEventSink
{
private readonly Lazy<ITransport> _transport;
private readonly Lazy<IGelfConverter> _converter;

public PeriodicBatchingGraylogSink(BatchingGraylogSinkOptions options) : this(options, options.BatchSizeLimit, options.Period, options.QueueLimit)
{

}

public PeriodicBatchingGraylogSink(BatchingGraylogSinkOptions options, int batchSizeLimit, TimeSpan period) : base(batchSizeLimit, period)
public PeriodicBatchingGraylogSink(BatchingGraylogSinkOptions options)
{
ISinkComponentsBuilder sinkComponentsBuilder = new SinkComponentsBuilder(options);
_transport = new Lazy<ITransport>(() => sinkComponentsBuilder.MakeTransport());
_converter = new Lazy<IGelfConverter>(() => sinkComponentsBuilder.MakeGelfConverter());

}

public PeriodicBatchingGraylogSink(BatchingGraylogSinkOptions options, int batchSizeLimit, TimeSpan period, int queueLimit) : base(batchSizeLimit, period, queueLimit)
public Task OnEmptyBatchAsync()
{
ISinkComponentsBuilder sinkComponentsBuilder = new SinkComponentsBuilder(options);
_transport = new Lazy<ITransport>(() => sinkComponentsBuilder.MakeTransport());
_converter = new Lazy<IGelfConverter>(() => sinkComponentsBuilder.MakeGelfConverter());
return Task.CompletedTask;
}

protected override void EmitBatch(IEnumerable<LogEvent> events)
Task IBatchedLogEventSink.EmitBatchAsync(IEnumerable<LogEvent> batch)
{
// ReSharper disable once PossibleMultipleEnumeration
try
{
Task[] sendTasks = events.Select(logEvent =>
IEnumerable<Task> sendTasks = batch.Select(logEvent =>
{
JObject json = _converter.Value.GetGelfJson(logEvent);
Task resultTask = _transport.Value.Send(json.ToString(Newtonsoft.Json.Formatting.None));
return resultTask;
}).ToArray();

var t = Task.WhenAll(sendTasks);
t.GetAwaiter().GetResult();
base.EmitBatch(events);
}
catch (Exception exc)
{
SelfLog.WriteLine("Oops something going wrong {0}", exc);
}
// ReSharper disable once PossibleMultipleEnumeration
}

protected override Task EmitBatchAsync(IEnumerable<LogEvent> events)
{
try
{
IEnumerable<Task> sendTasks = events.Select(logEvent =>
{
JObject json = _converter.Value.GetGelfJson(logEvent);
Task resultTask = _transport.Value.Send(json.ToString(Newtonsoft.Json.Formatting.None));
JsonObject json = _converter.Value.GetGelfJson(logEvent);
Task resultTask = _transport.Value.Send(json.ToString());
return resultTask;
});

Expand All @@ -78,11 +48,5 @@ protected override Task EmitBatchAsync(IEnumerable<LogEvent> events)
return Task.CompletedTask;
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_transport.Value?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net461;net46;</TargetFrameworks>
<!-- <TargetFrameworks>netstandard2.1;netstandard2.0;net5.0;net6.0;net7.0</TargetFrameworks>-->
<TargetFrameworks>net7.0</TargetFrameworks>
<!--Hack to include referenced project as content dll -->
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>

Expand Down Expand Up @@ -42,18 +43,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Serilog.Sinks.Graylog.Core\Serilog.Sinks.Graylog.Core.csproj" PrivateAssets="all" />
</ItemGroup>

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

<!--Hack to include referenced project as content dll -->
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void WhenCompressMessage_ThenResultShoouldBeExpected()
31,139,8,0,0,0,0,0,0,10,11,206,207,77,85,40,46,41,202,204,75,7,0,142,183,209,127,11,0,0,0
};

byte[] actual = giwen.Compress();
byte[] actual = giwen.ToGzip();
actual.Should().BeEquivalentTo(expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public void WhenCreateException_ThenBuildShoulNotThrow()

DateTimeOffset date = DateTimeOffset.Now;
LogEvent logEvent = LogEventSource.GetExceptionLogEvent(date, testExc);

//JObject obj = exceptionBuilder.Build(logEvent);

JObject obj = exceptionBuilder.Build(logEvent);

obj.Should().NotBeNull();
//obj.Should().NotBeNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void WhenGetSimpleEvent_ThenResult_ShouldBeExpected()
LogEvent logEvent = LogEventSource.GetSimpleLogEvent(date);

string expectedString = JsonConvert.SerializeObject(expected, Newtonsoft.Json.Formatting.None);
string actual = target.Build(logEvent).ToString(Newtonsoft.Json.Formatting.None);
//string actual = target.Build(logEvent).ToString(Newtonsoft.Json.Formatting.None);
//actual.ShouldBeEquivalentTo(expectedString);
}

Expand All @@ -52,7 +52,7 @@ public void TryComplexEvent()

LogEvent logEvent = LogEventSource.GetComplexEvent(date);

string actual = target.Build(logEvent).ToString(Newtonsoft.Json.Formatting.None);
//string actual = target.Build(logEvent).ToString(Newtonsoft.Json.Formatting.None);
}

[Fact]
Expand All @@ -61,19 +61,19 @@ public void GetSimpleLogEvent_GraylogSinkOptionsContainsHost_ReturnsOptionsHost(
//arrange
GraylogSinkOptions options = new GraylogSinkOptions()
{
Host = "my_host"
HostnameOverride = "my_host"
};
GelfMessageBuilder messageBuilder = new GelfMessageBuilder("localhost", options);
DateTime date = DateTime.UtcNow;
string expectedHost = "my_host";

//act
LogEvent logEvent = LogEventSource.GetSimpleLogEvent(date);
JObject actual = messageBuilder.Build(logEvent);
string actualHost = actual.Value<string>("host");
//JObject actual = messageBuilder.Build(logEvent);
//string actualHost = actual.Value<string>("host");

//assert
Assert.Equal(expectedHost, actualHost);
//Assert.Equal(expectedHost, actualHost);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />

<PackageReference Include="altcover" Version="8.2.833" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="altcover" Version="8.2.837" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Moq" Version="4.18.1" />

<ProjectReference Include="..\Serilog.Sinks.Graylog.Core\Serilog.Sinks.Graylog.Core.csproj" />
<ProjectReference Include="..\Serilog.Sinks.Graylog.Tests\Serilog.Sinks.Graylog.Tests.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ public void WhenSend_ThenCallMethods()
{
var transportClient = new Mock<ITransportClient<byte[]>>();
var dataToChunkConverter = new Mock<IDataToChunkConverter>();
var options = new GraylogSinkOptions();

var fixture = new Fixture();

var stringData = fixture.Create<string>();

byte[] data = stringData.Compress();
byte[] data = stringData.ToGzip();

List<byte[]> chunks = fixture.CreateMany<byte[]>(3).ToList();

dataToChunkConverter.Setup(c => c.ConvertToChunks(data)).Returns(chunks);

UdpTransport target = new UdpTransport(transportClient.Object, dataToChunkConverter.Object);
UdpTransport target = new UdpTransport(transportClient.Object, dataToChunkConverter.Object, options);

target.Send(stringData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Serilog.Sinks.Graylog.Core.Extensions
{
public static class StringExtensions
{
public static byte[] Compress(this string source)
public static byte[] ToGzip(this string source)
{
var resultStream = new MemoryStream();
using (var gzipStream = new GZipStream(resultStream, CompressionMode.Compress))
Expand All @@ -18,6 +18,8 @@ public static byte[] Compress(this string source)
return resultStream.ToArray();
}

public static byte[] ToByteArray(this string source) => System.Text.Encoding.UTF8.GetBytes(source);

/// <summary>
/// Truncates the specified maximum length.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Serilog.Sinks.Graylog.Core/GelfConverter.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
using Serilog.Events;
using Serilog.Sinks.Graylog.Core.MessageBuilders;

namespace Serilog.Sinks.Graylog.Core
{
public interface IGelfConverter
{
JObject GetGelfJson(LogEvent logEvent);
JsonObject GetGelfJson(LogEvent logEvent);
}

public class GelfConverter : IGelfConverter
Expand All @@ -20,7 +20,7 @@ public GelfConverter(IDictionary<BuilderType, Lazy<IMessageBuilder>> messageBuil
_messageBuilders = messageBuilders;
}

public JObject GetGelfJson(LogEvent logEvent)
public JsonObject GetGelfJson(LogEvent logEvent)
{
IMessageBuilder builder = logEvent.Exception != null
? _messageBuilders[BuilderType.Exception].Value
Expand Down
32 changes: 0 additions & 32 deletions src/Serilog.Sinks.Graylog.Core/GelfMessage.cs

This file was deleted.

Loading

0 comments on commit 9e99541

Please sign in to comment.