Skip to content

Commit

Permalink
Add option to include message templates as an additional field
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwcole committed Aug 4, 2019
1 parent 1de9506 commit adf9214
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Gelf.Extensions.Logging/GelfLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Internal;

namespace Gelf.Extensions.Logging
{
Expand All @@ -17,7 +16,8 @@ public class GelfLogger : ILogger
"logger",
"exception",
"event_id",
"event_name"
"event_name",
"message_template"
};

private readonly string _name;
Expand Down Expand Up @@ -113,8 +113,8 @@ public IDisposable BeginScope<TState>(TState state)

private static IEnumerable<KeyValuePair<string, object>> GetStateAdditionalFields<TState>(TState state)
{
return state is /*FormattedLogValues*/IEnumerable<KeyValuePair<string, object>> logValues
? logValues.Take(logValues.Count() - 1)
return state is IEnumerable<KeyValuePair<string, object>> logValues
? logValues
: Enumerable.Empty<KeyValuePair<string, object>>();
}

Expand All @@ -137,18 +137,25 @@ private IEnumerable<KeyValuePair<string, object>> GetScopeAdditionalFields()
return additionalFields.Reverse();
}

private static IEnumerable<KeyValuePair<string, object>> ValidateAdditionalFields(
private IEnumerable<KeyValuePair<string, object>> ValidateAdditionalFields(
IEnumerable<KeyValuePair<string, object>> additionalFields)
{
foreach (var field in additionalFields)
{
if (AdditionalFieldKeyRegex.IsMatch(field.Key) && !ReservedAdditionalFieldKeys.Contains(field.Key))
if (field.Key != "{OriginalFormat}")
{
yield return field;
if (AdditionalFieldKeyRegex.IsMatch(field.Key) && !ReservedAdditionalFieldKeys.Contains(field.Key))
{
yield return field;
}
else
{
Debug.Fail($"GELF message has additional field with invalid key \"{field.Key}\".");
}
}
else
else if (_options.IncludeMessageTemplates)
{
Debug.Fail($"GELF message has additional field with invalid key \"{field.Key}\".");
yield return new KeyValuePair<string, object>("message_template", field.Value);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Gelf.Extensions.Logging/GelfLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ public GelfLoggerOptions()
/// Timeout used when sending logs via HTTP(S).
/// </summary>
public TimeSpan HttpTimeout { get; set; } = TimeSpan.FromSeconds(30);

/// <summary>
/// Include a field with the original message template before structured log parameters are replaced.
/// </summary>
public bool IncludeMessageTemplates { get; set; }
}
}
22 changes: 22 additions & 0 deletions test/Gelf.Extensions.Logging.Tests/GelfLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async Task Sends_message_to_Graylog(LogLevel logLevel, int expectedLevel)
Assert.Equal(expectedLevel, message.level);
Assert.Equal(typeof(GelfLoggerTests).FullName, message.logger);
Assert.Throws<RuntimeBinderException>(() => message.exception);
Assert.Throws<RuntimeBinderException>(() => message.message_template);
}

[Fact]
Expand Down Expand Up @@ -216,6 +217,27 @@ public async Task Ignores_null_values_in_additional_fields()
}
}

[Fact]
public async Task Sends_message_templates_when_enabled()
{
var options = LoggerFixture.LoggerOptions;
options.IncludeMessageTemplates = true;
options.AdditionalFields.Add("test_id", TestContext.TestId);

using (var loggerFactory = LoggerFixture.CreateLoggerFactory(options))
{
var messageTemplate = "This is a message template {foo} {bar}";
var sut = loggerFactory.CreateLogger(nameof(GelfLoggerTests));
sut.LogInformation(messageTemplate, "FOO", "BAR");

var message = await GraylogFixture.WaitForMessageAsync();

Assert.Equal(messageTemplate, message.message_template);
Assert.Equal("FOO", message.foo);
Assert.Equal("BAR", message.bar);
}
}

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

0 comments on commit adf9214

Please sign in to comment.