From 1715442daba090d0d2d2012ea3102bb8bf485c67 Mon Sep 17 00:00:00 2001 From: Marty Tippin <120425148+tippmar-nr@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:55:20 -0600 Subject: [PATCH] Optimize ThreadIdEnricher --- .../NewRelic/Agent/Core/Logging/ThreadIdEnricher.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Core/Logging/ThreadIdEnricher.cs b/src/Agent/NewRelic/Agent/Core/Logging/ThreadIdEnricher.cs index df094cbd90..93ae9e0e86 100644 --- a/src/Agent/NewRelic/Agent/Core/Logging/ThreadIdEnricher.cs +++ b/src/Agent/NewRelic/Agent/Core/Logging/ThreadIdEnricher.cs @@ -14,18 +14,15 @@ namespace NewRelic.Agent.Core [NrExcludeFromCodeCoverage] internal class ThreadIdEnricher : ILogEventEnricher { - private LogEventProperty _tidProperty; + + private static ThreadLocal _tidProperty = new ThreadLocal(); public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - var threadId = Thread.CurrentThread.ManagedThreadId; - - var prop = _tidProperty; - - if (prop == null || (int?)((ScalarValue)prop.Value).Value != threadId) - _tidProperty = prop = propertyFactory.CreateProperty("tid", threadId); + if (!_tidProperty.IsValueCreated) + _tidProperty.Value = propertyFactory.CreateProperty("tid", Thread.CurrentThread.ManagedThreadId); - logEvent.AddPropertyIfAbsent(prop); + logEvent.AddPropertyIfAbsent(_tidProperty.Value); } } }