Skip to content
Open
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
34 changes: 33 additions & 1 deletion src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,29 @@ protected override void InitializeLayout()
UrlUserName = "${aspnet-user-identity}";
}

_staticTags = AllowStaticCapture(Tags) ? GetTags(LogEventInfo.CreateNullEvent()) : null;
_staticLabels = AllowStaticCapture(Labels) ? GetLabels(LogEventInfo.CreateNullEvent()) : null;
_staticMetadata = AllowStaticCapture(Metadata) && !IncludeEventProperties && !IncludeScopeProperties ? GetMetadata(LogEventInfo.CreateNullEvent()) : null;

base.InitializeLayout();
}

private static bool AllowStaticCapture(IList<TargetPropertyWithContext> items)
{
if (items?.Count > 0)
{
foreach (var item in items)
{
if (item.Layout is SimpleLayout simpleLayout && simpleLayout.IsFixedText)
continue;

return false;
}
return true;
}
return false;
}

private static Lazy<bool> NLogApmLoaded { get; } = new Lazy<bool>(() => Type.GetType("Elastic.Apm.NLog.ApmTraceIdLayoutRenderer, Elastic.Apm.NLog") != null);

#if NETFRAMEWORK
Expand Down Expand Up @@ -349,6 +369,9 @@ protected override string GetFormattedMessage(LogEventInfo logEvent)

private MetadataDictionary GetMetadata(LogEventInfo e)
{
if (_staticMetadata != null)
return _staticMetadata;

if ((!IncludeEventProperties || !e.HasProperties) && Metadata?.Count == 0 && !IncludeScopeProperties)
return null;

Expand Down Expand Up @@ -402,8 +425,9 @@ private MetadataDictionary GetMetadata(LogEventInfo e)
? metadata
: null;
}
private MetadataDictionary _staticMetadata;

private bool AllowSerializePropertyValue(string propertyName, global::NLog.MessageTemplates.MessageTemplateParameters templateParameters)
private static bool AllowSerializePropertyValue(string propertyName, global::NLog.MessageTemplates.MessageTemplateParameters templateParameters)
{
if (templateParameters.Count > 0 && !templateParameters.IsPositional)
{
Expand Down Expand Up @@ -451,6 +475,9 @@ private Log GetLog(LogEventInfo logEventInfo)

private string[] GetTags(LogEventInfo e)
{
if (_staticTags != null)
return _staticTags;

if (Tags.Count == 0)
return null;

Expand All @@ -471,6 +498,7 @@ private string[] GetTags(LogEventInfo e)
? tags.ToArray()
: null;
}
private string[] _staticTags;

private static string[] GetTagsSplit(string tags) =>
string.IsNullOrEmpty(tags)
Expand All @@ -479,6 +507,9 @@ private static string[] GetTagsSplit(string tags) =>

private Labels GetLabels(LogEventInfo e)
{
if (_staticLabels != null)
return _staticLabels;

if (Labels.Count == 0)
return null;

Expand All @@ -493,6 +524,7 @@ private Labels GetLabels(LogEventInfo e)
? labels
: null;
}
private Labels _staticLabels;

private Event GetEvent(LogEventInfo logEventInfo)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
<ProjectReference Include="..\Elastic.CommonSchema\Elastic.CommonSchema.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="6.1.0" />
<PackageReference Include="NLog" Version="6.1.1" />
</ItemGroup>
</Project>
Loading