Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 919b919

Browse files
Compare both Id and Name for equity.
1 parent 4eb8b3c commit 919b919

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

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

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,94 @@ internal sealed class EventIdPropertyCache
1010
{
1111
private readonly object _createLock = new();
1212
private readonly int _maxCapacity;
13-
private readonly Dictionary<int, LogEventProperty> _propertyCache;
13+
private readonly Dictionary<EventKey, LogEventProperty> _propertyCache;
1414

1515
private int count;
1616

1717
public EventIdPropertyCache(int maxCapacity)
1818
{
19-
this._maxCapacity = maxCapacity;
20-
this._propertyCache = new Dictionary<int, LogEventProperty>(capacity: maxCapacity);
19+
_maxCapacity = maxCapacity;
20+
_propertyCache = new Dictionary<EventKey, LogEventProperty>(capacity: maxCapacity);
2121
}
2222

2323
public LogEventProperty GetOrCreateProperty(in EventId eventId)
2424
{
25-
if (_propertyCache.TryGetValue(eventId.Id, out var cachedProperty))
25+
var eventKey = new EventKey(eventId);
26+
27+
if (_propertyCache.TryGetValue(eventKey, out var cachedProperty))
2628
{
2729
return cachedProperty;
2830
}
2931

3032
lock (_createLock)
3133
{
32-
return GetOrCreateSynchronized(in eventId);
34+
// Double check under lock
35+
if (_propertyCache.TryGetValue(eventKey, out cachedProperty))
36+
{
37+
return cachedProperty;
38+
}
39+
40+
cachedProperty = CreateCore(in eventKey);
41+
42+
if (count < _maxCapacity)
43+
{
44+
_propertyCache[eventKey] = cachedProperty;
45+
count++;
46+
}
47+
48+
return cachedProperty;
3349
}
3450
}
3551

36-
private static LogEventProperty CreateCore(in EventIdeventId)
52+
private static LogEventProperty CreateCore(in EventKeyeventKey)
3753
{
3854
var properties = new List<LogEventProperty>(2);
3955

40-
if (eventId.Id != 0)
56+
if (eventKey.Id != 0)
4157
{
42-
properties.Add(new LogEventProperty("Id", new ScalarValue(eventId.Id)));
58+
properties.Add(new LogEventProperty("Id", new ScalarValue(eventKey.Id)));
4359
}
4460

45-
if (eventId.Name != null)
61+
if (eventKey.Name != null)
4662
{
47-
properties.Add(new LogEventProperty("Name", new ScalarValue(eventId.Name)));
63+
properties.Add(new LogEventProperty("Name", new ScalarValue(eventKey.Name)));
4864
}
4965

5066
return new LogEventProperty("EventId", new StructureValue(properties));
5167
}
5268

53-
private LogEventPropertyGetOrCreateSynchronized(inEventIdeventId)
69+
private readonlystructEventKey:IEquatable<EventKey>
5470
{
55-
// Double check under lock
56-
if(_propertyCache.TryGetValue(eventId.Id,outvarcachedProperty))
71+
72+
publicEventKey(EventIdeventId)
5773
{
58-
return cachedProperty;
74+
Id = eventId.Id;
75+
Name = eventId.Name;
5976
}
6077

61-
cachedProperty=CreateCore(ineventId);
78+
publicintId{get;}
6279

63-
if (count < _maxCapacity)
80+
public string? Name { get; }
81+
82+
/// <inheritdoc />
83+
public override int GetHashCode()
6484
{
65-
_propertyCache[eventId.Id] = cachedProperty;
66-
count++;
85+
unchecked
86+
{
87+
var hashCode = 17;
88+
89+
hashCode = (hashCode * 397) ^ this.Id;
90+
hashCode = (hashCode * 397) ^ (this.Name?.GetHashCode() ?? 0);
91+
92+
return hashCode;
93+
}
6794
}
68-
69-
return cachedProperty;
95+
96+
/// <inheritdoc />
97+
public bool Equals(EventKey other) => this.Id == other.Id && this.Name == other.Name;
98+
99+
/// <inheritdoc />
100+
public override bool Equals(object? obj) => obj is EventKey other && Equals(other);
70101
}
71102
}
72103
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /