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..3951cb26 100644 --- a/src/Elastic.CommonSchema.NLog/Elastic.CommonSchema.NLog.csproj +++ b/src/Elastic.CommonSchema.NLog/Elastic.CommonSchema.NLog.csproj @@ -11,6 +11,6 @@ - +