Skip to content

Commit cb77647

Browse files
Shared static EventIdPropertyCache cache; Use ConcurrentDictionary as storage.
1 parent 59165d5 commit cb77647

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

src/Serilog.Extensions.Logging/Extensions/Logging/EventIdPropertyCache.cs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,46 @@
1414

1515
namespace Serilog.Extensions.Logging
1616
{
17+
using System.Collections.Concurrent;
1718
using Microsoft.Extensions.Logging;
1819
using Serilog.Events;
1920

20-
internal sealed class EventIdPropertyCache
21+
static class EventIdPropertyCache
2122
{
22-
private readonly object _createLock = new();
23-
private readonly int _maxCapacity;
24-
private readonly Dictionary<EventKey, LogEventProperty> _propertyCache;
23+
const int MaxCachedProperties = 1024;
2524

26-
private int count;
25+
static readonly ConcurrentDictionary<EventKey, LogEventProperty> s_propertyCache = new();
26+
static int s_count;
2727

28-
public EventIdPropertyCache(int maxCapacity)
29-
{
30-
_maxCapacity = maxCapacity;
31-
_propertyCache = new Dictionary<EventKey, LogEventProperty>(capacity: maxCapacity);
32-
}
33-
34-
public LogEventProperty GetOrCreateProperty(in EventId eventId)
28+
public static LogEventProperty GetOrCreateProperty(in EventId eventId)
3529
{
3630
var eventKey = new EventKey(eventId);
3731

38-
if (_propertyCache.TryGetValue(eventKey, out var cachedProperty))
39-
{
40-
return cachedProperty;
41-
}
32+
LogEventProperty? property;
4233

43-
lock (_createLock)
34+
if (s_count >= MaxCachedProperties)
4435
{
45-
// Double check under lock
46-
if (_propertyCache.TryGetValue(eventKey, out cachedProperty))
36+
if (!s_propertyCache.TryGetValue(eventKey, out property))
4737
{
48-
return cachedProperty;
38+
property = CreateCore(in eventKey);
4939
}
50-
51-
cachedProperty = CreateCore(in eventKey);
52-
53-
if (count < _maxCapacity)
54-
{
55-
_propertyCache[eventKey] = cachedProperty;
56-
count++;
57-
}
58-
59-
return cachedProperty;
6040
}
41+
else
42+
{
43+
property = s_propertyCache.GetOrAdd(
44+
eventKey,
45+
static key =>
46+
{
47+
Interlocked.Increment(ref s_count);
48+
49+
return CreateCore(in key);
50+
});
51+
}
52+
53+
return property;
6154
}
6255

63-
private static LogEventProperty CreateCore(in EventKey eventKey)
56+
static LogEventProperty CreateCore(in EventKey eventKey)
6457
{
6558
var properties = new List<LogEventProperty>(2);
6659

@@ -77,9 +70,8 @@ private static LogEventProperty CreateCore(in EventKey eventKey)
7770
return new LogEventProperty("EventId", new StructureValue(properties));
7871
}
7972

80-
private readonly record struct EventKey
73+
readonly record struct EventKey
8174
{
82-
8375
public EventKey(EventId eventId)
8476
{
8577
Id = eventId.Id;

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ internal static string GetKeyWithoutFirstSymbol(ConcurrentDictionary<string, str
2929
readonly SerilogLoggerProvider _provider;
3030
readonly ILogger _logger;
3131

32-
readonly EventIdPropertyCache _eventPropertyCache = new (48);
33-
34-
static readonly CachingMessageTemplateParser MessageTemplateParser = new();
32+
static readonly CachingMessageTemplateParser MessageTemplateParser = new ();
3533

3634
public SerilogLogger(
3735
SerilogLoggerProvider provider,
@@ -152,7 +150,7 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
152150
}
153151

154152
if (eventId.Id != 0 || eventId.Name != null)
155-
properties.Add(this._eventPropertyCache.GetOrCreateProperty(in eventId));
153+
properties.Add(EventIdPropertyCache.GetOrCreateProperty(in eventId));
156154

157155
var (traceId, spanId) = Activity.Current is { } activity ?
158156
(activity.TraceId, activity.SpanId) :

test/Serilog.Extensions.Logging.Tests/EventIdPropertyCacheTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ public class EventIdPropertyCacheTests
2828
public void LowAndHighNumberedEventIdsAreMapped(int id)
2929
{
3030
// Arrange
31-
var cache = new EventIdPropertyCache(48);
32-
3331
var eventId = new EventId(id, "test");
3432

3533
// Act
36-
var mapped = cache.GetOrCreateProperty(eventId);
34+
var mapped = EventIdPropertyCache.GetOrCreateProperty(eventId);
3735

3836
// Assert
3937
var value = Assert.IsType<StructureValue>(mapped.Value);

0 commit comments

Comments
 (0)