Skip to content

Commit

Permalink
Optimize ThreadIdEnricher
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Nov 17, 2023
1 parent e85ff7a commit 1715442
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Agent/NewRelic/Agent/Core/Logging/ThreadIdEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ namespace NewRelic.Agent.Core
[NrExcludeFromCodeCoverage]
internal class ThreadIdEnricher : ILogEventEnricher
{
private LogEventProperty _tidProperty;

private static ThreadLocal<LogEventProperty> _tidProperty = new ThreadLocal<LogEventProperty>();

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);
}
}
}

0 comments on commit 1715442

Please sign in to comment.