Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLog EcsLayout - Add support for ExcludeProperties #94

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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