Skip to content

Commit 7ced0e2

Browse files
authored
Merge pull request #62 from evilpilaf/nulls
Ignore properties with null values
2 parents 97c1c98 + c988a16 commit 7ced0e2

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[*]
77
indent_style = space
88
indent_size = 2
9+
end_of_line = lf
910

1011
# Code files
1112
[*.{cs,csx,vb,vbx}]

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* text=auto
1+
* text eol=lf

assets/icon124x124.png

-1 Bytes
Loading

src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,17 @@ public static void FormatContent(LogEvent logEvent, TextWriter output)
5353
private static void WriteProperties(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output)
5454
{
5555
const string precedingDelimiter = ",";
56-
foreach (var property in properties)
56+
foreach (var property in properties.Where(p => p.Value != null && !string.IsNullOrWhiteSpace(p.Value.ToString())))
5757
{
58+
// Skip properties with empty values
59+
if (property.Value is ScalarValue v)
60+
{
61+
if (v.Value == null || v.Value.ToString().Equals(""))
62+
{
63+
continue;
64+
}
65+
}
66+
5867
output.Write(precedingDelimiter);
5968

6069
JsonValueFormatter.WriteQuotedJsonString(property.Key, output);

src/Honeycomb.Serilog.Sink/HoneycombSerilogSink.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
5757
await SendBatchedEvents(writer!.ToString()).ConfigureAwait(false);
5858
}
5959

60+
// ReSharper disable UnusedMember.Local
6061
private async Task SendBatchedEvents(Stream events)
62+
// ReSharper restore UnusedMember.Local
6163
{
6264
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, string.Format(HoneycombBatchEndpointTemplate, _teamId))
6365
{

test/Honeycomb.Serilog.Sink.Tests/Honeycomb.Serilog.Sink.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
<PrivateAssets>all</PrivateAssets>
1515
</PackageReference>
16-
<PackageReference Include="coverlet.collector" Version="3.0.1">
16+
<PackageReference Include="coverlet.collector" Version="3.0.2">
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
<PrivateAssets>all</PrivateAssets>
1919
</PackageReference>

test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
using Serilog.Events;
1414
using Serilog.Parsing;
15-
using Serilog.Sinks.PeriodicBatching;
1615

1716
using Xunit;
1817

@@ -186,6 +185,45 @@ public async Task Emit_GivenAMessageWithProperties_SendsThemAllAsync()
186185
}
187186
}
188187

188+
[Theory]
189+
[InlineData("")]
190+
[InlineData(null)]
191+
public async Task Emit_GivenAMessageWithEmptyPropertyValue_SkipsSendingProperty(string property)
192+
{
193+
const string dataset = nameof(dataset);
194+
const string apiKey = nameof(apiKey);
195+
196+
HttpClientStub clientStub = A.HttpClient();
197+
198+
var sut = CreateSut(dataset, apiKey, clientStub);
199+
200+
var level = LogEventLevel.Fatal;
201+
202+
var messageTemplateString = $"Testing message property {{{nameof(property)}}}";
203+
204+
var eventToSend = Some.LogEvent(level, messageTemplateString, property);
205+
206+
await sut.EmitTestable(eventToSend);
207+
208+
var requestContent = clientStub.RequestContent;
209+
using (var document = JsonDocument.Parse(requestContent))
210+
using (new AssertionScope())
211+
{
212+
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
213+
document.RootElement.GetArrayLength().Should().Be(1);
214+
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();
215+
216+
sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
217+
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);
218+
219+
JsonElement data = sentEvent.GetProperty("data");
220+
221+
var exists = data.TryGetProperty(nameof(property), out _);
222+
223+
exists.Should().BeFalse();
224+
}
225+
}
226+
189227
private HoneycombSerilogSinkStub CreateSut(string dataset, string apiKey, HttpClient client = null)
190228
{
191229
return new HoneycombSerilogSinkStub(client, dataset, apiKey);

0 commit comments

Comments
 (0)