Skip to content

Commit 49302b8

Browse files
committed
NLog EcsLayout now converts properties of type Enum as string
1 parent b380ac7 commit 49302b8

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/Elastic.CommonSchema.NLog/EcsLayout.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,7 @@ private MetadataDictionary GetMetadata(LogEventInfo e)
383383
continue;
384384

385385
var propertyValue = prop.Value;
386-
if (propertyValue is null or IConvertible || propertyValue.GetType().IsValueType)
387-
Populate(metadata, propertyName, propertyValue);
388-
else
386+
if (!TryPopulateWhenSafe(metadata, propertyName, propertyValue))
389387
{
390388
templateParameters ??= e.MessageTemplateParameters;
391389
var value = AllowSerializePropertyValue(propertyName, templateParameters) ? propertyValue : propertyValue.ToString();
@@ -402,7 +400,10 @@ private MetadataDictionary GetMetadata(LogEventInfo e)
402400
continue;
403401

404402
var propertyValue = MappedDiagnosticsLogicalContext.GetObject(key);
405-
Populate(metadata, key, propertyValue);
403+
if (!TryPopulateWhenSafe(metadata, key, propertyValue))
404+
{
405+
Populate(metadata, key, propertyValue.ToString());
406+
}
406407
}
407408
}
408409

@@ -722,6 +723,19 @@ private static long GetSysLogSeverity(LogLevel logLevel)
722723
return 2; // LogLevel.Fatal
723724
}
724725

726+
private static bool TryPopulateWhenSafe(IDictionary<string, object> propertyBag, string key, object value)
727+
{
728+
if (value is null or IConvertible || value.GetType().IsValueType)
729+
{
730+
if (value is Enum)
731+
value = value.ToString();
732+
Populate(propertyBag, key, value);
733+
return true;
734+
}
735+
736+
return false;
737+
}
738+
725739
private static void Populate(IDictionary<string, object> propertyBag, string key, object value)
726740
{
727741
if (string.IsNullOrEmpty(key))

tests-integration/Elastic.NLog.Targets.IntegrationTests/LoggingToDataStreamTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public async Task LogsEndUpInCluster()
6262
loggedError.Ecs.Version.Should().Be(EcsDocument.Version);
6363
loggedError.Ecs.Version.Should().NotStartWith("v");
6464

65-
loggedError.Metadata.Should().ContainKey("Status");
66-
loggedError.Metadata["Status"].Should().Be((int)MyEnum.Failure);
65+
loggedError.Labels.Should().ContainKey("Status");
66+
loggedError.Labels["Status"].Should().Be("Failure");
6767
}
6868
}
6969
}

0 commit comments

Comments
 (0)