in src/Elastic.CommonSchema.NLog/EcsLayout.cs [366:421]
private MetadataDictionary GetMetadata(LogEventInfo e)
{
if ((!IncludeEventProperties || !e.HasProperties) && Metadata?.Count == 0 && !IncludeScopeProperties)
return null;
var metadata = new MetadataDictionary();
if (IncludeEventProperties && e.HasProperties)
{
global::NLog.MessageTemplates.MessageTemplateParameters templateParameters = null;
foreach (var prop in e.Properties)
{
var propertyName = prop.Key?.ToString();
if (string.IsNullOrEmpty(propertyName) || ExcludeProperties.Contains(propertyName))
continue;
var propertyValue = prop.Value;
if (!TryPopulateWhenSafe(metadata, propertyName, propertyValue))
{
templateParameters ??= e.MessageTemplateParameters;
var value = AllowSerializePropertyValue(propertyName, templateParameters) ? propertyValue : propertyValue.ToString();
Populate(metadata, propertyName, value);
}
}
}
if (IncludeScopeProperties)
{
foreach (var key in MappedDiagnosticsLogicalContext.GetNames())
{
if (string.IsNullOrEmpty(key) || ExcludeProperties.Contains(key))
continue;
var propertyValue = MappedDiagnosticsLogicalContext.GetObject(key);
if (!TryPopulateWhenSafe(metadata, key, propertyValue))
{
Populate(metadata, key, propertyValue.ToString());
}
}
}
if (Metadata?.Count > 0)
{
foreach (var targetPropertyWithContext in Metadata)
{
var value = targetPropertyWithContext.Layout?.Render(e);
if (targetPropertyWithContext.IncludeEmptyValue || !string.IsNullOrEmpty(value))
Populate(metadata, targetPropertyWithContext.Name, value);
}
}
return metadata.Count > 0
? metadata
: null;
}