Skip to content

Commit

Permalink
Ignore null additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwcole committed May 8, 2018
1 parent f919433 commit e9dc63b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public ValuesController(ILogger<ValuesController> logger)
[HttpGet]
public IEnumerable<string> Get()
{
using (_logger.BeginScope(("method_name", nameof(Get))))
using (_logger.BeginScope(("scope_field", "foo")))
{
var result = new[] {"foo", "bar"};
var result = new[] {"bar", "baz"};

_logger.LogInformation("Returning {value1} and {value2} from controller", result[0], result[1]);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions samples/Gelf.Extensions.Logging.Samples.NetCore1/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -12,7 +11,6 @@ public class Program
public static void Main()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions samples/Gelf.Extensions.Logging.Samples.NetCore2/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -12,7 +11,6 @@ public class Program
public static void Main()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();

Expand Down
2 changes: 1 addition & 1 deletion src/Gelf.Extensions.Logging/UdpGelfClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static byte[] GetMessageBytes(GelfMessage message)

foreach (var field in message.AdditionalFields)
{
messageJson[$"_{field.Key}"] = field.Value as string ?? field.Value.ToString();
messageJson[$"_{field.Key}"] = field.Value?.ToString();
}

return Encoding.UTF8.GetBytes(messageJson.ToString());
Expand Down
27 changes: 27 additions & 0 deletions test/Gelf.Extensions.Logging.Tests/GelfLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ public async Task Sends_message_with_additional_fields_from_structured_log()
Assert.Equal("bar", message.second_value);
}

[Fact]
public async Task Ignores_null_values_in_additional_fields()
{
var options = _loggerFixture.LoggerOptions;
options.AdditionalFields["foo"] = null;

using (var loggerFactory = _loggerFixture.CreateLoggerFactory(options))
{
var sut = loggerFactory.CreateLogger(nameof(GelfLoggerTests));

using (sut.BeginScope(("bar", (string) null)))
using (sut.BeginScope(new Dictionary<string, object>
{
["baz"] = null
}))
{
sut.LogInformation(_faker.Lorem.Sentence());
}

var message = await _graylogFixture.WaitForMessageAsync();

Assert.Throws<RuntimeBinderException>(() => message.foo);
Assert.Throws<RuntimeBinderException>(() => message.bar);
Assert.Throws<RuntimeBinderException>(() => message.baz);
}
}

public void Dispose()
{
_loggerFixture.Dispose();
Expand Down

0 comments on commit e9dc63b

Please sign in to comment.