Skip to content

Commit

Permalink
NLog EcsLayout - Add support for ExcludeProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Jul 6, 2020
1 parent b3f165e commit d289cef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public EcsLayout()
[ArrayParameter(typeof(TargetPropertyWithContext), "tag")]
public IList<TargetPropertyWithContext> Tags { get; } = new List<TargetPropertyWithContext>();

/// <summary>
/// List of property names to exclude from MetaData, when <see cref="IncludeAllProperties"/> is true
/// </summary>
public ISet<string> ExcludeProperties { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
{
var ecsEvent = new Base
Expand Down Expand Up @@ -219,7 +224,13 @@ private IDictionary<string, object> GetMetadata(LogEventInfo e)
if (IncludeAllProperties && e.HasProperties)
{
foreach (var prop in e.Properties)
Populate(metadata, prop.Key?.ToString(), prop.Value);
{
var propertyName = prop.Key?.ToString();
if (string.IsNullOrEmpty(propertyName) || ExcludeProperties.Contains(propertyName))
continue;

Populate(metadata, propertyName, prop.Value);
}
}

if (IncludeMdlc)
Expand All @@ -239,7 +250,7 @@ private IDictionary<string, object> GetMetadata(LogEventInfo e)
foreach (var targetPropertyWithContext in Metadata)
{
var value = targetPropertyWithContext.Layout?.Render(e);
if (!string.IsNullOrEmpty(value) || targetPropertyWithContext.IncludeEmptyValue)
if (targetPropertyWithContext.IncludeEmptyValue || !string.IsNullOrEmpty(value))
Populate(metadata, targetPropertyWithContext.Name, value);
}
}
Expand Down Expand Up @@ -483,7 +494,7 @@ private static void Populate(IDictionary<string, object> propertyBag, string key

while (propertyBag.ContainsKey(key))
{
if (string.Equals(value.ToString(), propertyBag[key].ToString(), StringComparison.Ordinal))
if (string.Equals(value?.ToString(), propertyBag[key]?.ToString(), StringComparison.Ordinal))
return;

key += "_1";
Expand Down
4 changes: 3 additions & 1 deletion tests/Elastic.CommonSchema.NLog.Tests/LogTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ protected static void TestLogger(Action<ILogger, Func<List<string>>> act)

var logFactory = new LogFactory();
var logConfig = new Config.LoggingConfiguration(logFactory);
var memoryTarget = new MemoryTarget { Layout = new EcsLayout(), OptimizeBufferReuse = true };
var ecsLayout = new EcsLayout();
ecsLayout.ExcludeProperties.Add("NotX");
var memoryTarget = new MemoryTarget { Layout = ecsLayout, OptimizeBufferReuse = true };
logConfig.AddRule(LogLevel.Trace, LogLevel.Fatal, memoryTarget);
logConfig.DefaultCultureInfo = System.Globalization.CultureInfo.InvariantCulture;
logFactory.Configuration = logConfig;
Expand Down
5 changes: 3 additions & 2 deletions tests/Elastic.CommonSchema.NLog.Tests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ public void SeesMessage() => TestLogger((logger, getLogEvents) =>
[Fact]
public void SeesMessageWithProp() => TestLogger((logger, getLogEvents) =>
{
logger.Info("Info {ValueX} {SomeY}", "X", 2.2);
logger.Info("Info {ValueX} {SomeY} {NotX}", "X", 2.2, 42);

var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);

var ecsEvents = ToEcsEvents(logEvents);

var (_, info) = ecsEvents.First();
info.Message.Should().Be("Info \"X\" 2.2");
info.Message.Should().Be("Info \"X\" 2.2 42");
info.Metadata.Should().ContainKey("value_x");
info.Metadata.Should().ContainKey("some_y");
info.Metadata.Should().NotContainKey("not_x");

var x = info.Metadata["value_x"] as string;
x.Should().NotBeNull().And.Be("X");
Expand Down

0 comments on commit d289cef

Please sign in to comment.