diff --git a/src/Elastic.Apm.NLog/Elastic.Apm.NLog.csproj b/src/Elastic.Apm.NLog/Elastic.Apm.NLog.csproj
index 0141d7bb..4b664695 100644
--- a/src/Elastic.Apm.NLog/Elastic.Apm.NLog.csproj
+++ b/src/Elastic.Apm.NLog/Elastic.Apm.NLog.csproj
@@ -13,7 +13,7 @@
Feel free to open an issue on our repos to discuss if you feel you need to target
a lower NLog version
-->
-
+
diff --git a/src/Elastic.CommonSchema.NLog/EcsLayout.cs b/src/Elastic.CommonSchema.NLog/EcsLayout.cs
index c7583220..cf2db307 100644
--- a/src/Elastic.CommonSchema.NLog/EcsLayout.cs
+++ b/src/Elastic.CommonSchema.NLog/EcsLayout.cs
@@ -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 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 NLogApmLoaded { get; } = new Lazy(() => Type.GetType("Elastic.Apm.NLog.ApmTraceIdLayoutRenderer, Elastic.Apm.NLog") != null);
#if NETFRAMEWORK
@@ -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;
@@ -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)
{
@@ -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;
@@ -471,6 +498,7 @@ private string[] GetTags(LogEventInfo e)
? tags.ToArray()
: null;
}
+ private string[] _staticTags;
private static string[] GetTagsSplit(string tags) =>
string.IsNullOrEmpty(tags)
@@ -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;
@@ -493,6 +524,7 @@ private Labels GetLabels(LogEventInfo e)
? labels
: null;
}
+ private Labels _staticLabels;
private Event GetEvent(LogEventInfo logEventInfo)
{
diff --git a/src/Elastic.CommonSchema.NLog/Elastic.CommonSchema.NLog.csproj b/src/Elastic.CommonSchema.NLog/Elastic.CommonSchema.NLog.csproj
index df238ce4..48d08a39 100644
--- a/src/Elastic.CommonSchema.NLog/Elastic.CommonSchema.NLog.csproj
+++ b/src/Elastic.CommonSchema.NLog/Elastic.CommonSchema.NLog.csproj
@@ -11,6 +11,6 @@
-
+
diff --git a/tests/Elastic.Apm.NLog.Tests/Elastic.Apm.NLog.Tests.csproj b/tests/Elastic.Apm.NLog.Tests/Elastic.Apm.NLog.Tests.csproj
index d421ae4e..6616756b 100644
--- a/tests/Elastic.Apm.NLog.Tests/Elastic.Apm.NLog.Tests.csproj
+++ b/tests/Elastic.Apm.NLog.Tests/Elastic.Apm.NLog.Tests.csproj
@@ -7,7 +7,7 @@
-
+
diff --git a/tests/Elastic.CommonSchema.NLog.Tests/Elastic.CommonSchema.NLog.Tests.csproj b/tests/Elastic.CommonSchema.NLog.Tests/Elastic.CommonSchema.NLog.Tests.csproj
index e9814132..bc266df3 100644
--- a/tests/Elastic.CommonSchema.NLog.Tests/Elastic.CommonSchema.NLog.Tests.csproj
+++ b/tests/Elastic.CommonSchema.NLog.Tests/Elastic.CommonSchema.NLog.Tests.csproj
@@ -8,7 +8,7 @@
-
+