mattwcole / gelf-extensions-logging

GELF provider for Microsoft.Extensions.Logging
MIT License
109 stars 42 forks source link

GrayLog errors because SysLogSeverity serialized as string #31

Closed ericcoleman closed 5 years ago

ericcoleman commented 5 years ago

We're looking at using this Library for logging in our Asp.Net Core 2.1 web api, but we're getting an error in GrayLog.

WARN [Messages] Failed to index message: index=<graylog_7> id=<0ac6b401-1395-11e9-b13e-062322a0902c> error=<{"type":"mapper_parsing_exception","reason":"failed to parse [level]","caused_by":{"type":"number_format_exception","reason":"For input string: \"Informational\""}}>

Our default JSON settings use the StringEnumConverter which is causing the SysLogSeverity to not be serialized to an integer.

I believe the fix for this is to write a custom JsonConverter for the SysLogSeverity enum, but wanted to get your input on this.

mattwcole commented 5 years ago

Thanks for reporting this. It looks like the library is incorrectly using the shared default Json.NET settings. I believe this failing test demonstrates the error.

[Fact]
public void Ignores_default_settings_while_serialising_to_JSON()
{
    JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    {
        Converters =
        {
            new StringEnumConverter()
        }
    };

    var message = new GelfMessage
    {
        Level = SyslogSeverity.Emergency,
        AdditionalFields = new Dictionary<string, object>()
    };

    var messageJson = message.ToJson();

    Assert.DoesNotContain("Emergency", messageJson);
}

I should be able to push out a fix in the next few days, or feel free to put up a PR.

mattwcole commented 5 years ago

This should do the job. I've published v1.5.1-pre1 with the fix. Let me know if that solves it.

ericcoleman commented 5 years ago

👍 Just tried it out and that fixes the issue. Thanks for your quick response.

mattwcole commented 5 years ago

No problem, I'll release a stable version shortly.