Skip to content

Commit 4eb8b3c

Browse files
Use EventIdPropertyCache in SerilogLogger; Create EventIdPropertyCache test class.
1 parent 0e5fe4c commit 4eb8b3c

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

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

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

32-
static readonly CachingMessageTemplateParser MessageTemplateParser = new();
32+
/// Per-ILogger LogEventProperty cache for EventId.
33+
/// Each ILogger has its own cache.
34+
/// Each { ILogger, EventId.Id } pair is assumed to be unique.
35+
readonly EventIdPropertyCache _eventPropertyCache = new (48);
3336

34-
// It's rare to see large event ids, as they are category-specific
35-
static readonly LogEventProperty[] LowEventIdValues = Enumerable.Range(0, 48)
36-
.Select(n => new LogEventProperty("Id", new ScalarValue(n)))
37-
.ToArray();
37+
static readonly CachingMessageTemplateParser MessageTemplateParser = new();
3838

3939
public SerilogLogger(
4040
SerilogLoggerProvider provider,
@@ -155,7 +155,7 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
155155
}
156156

157157
if (eventId.Id != 0 || eventId.Name != null)
158-
properties.Add(CreateEventIdProperty(eventId));
158+
properties.Add(this._eventPropertyCache.GetOrCreateProperty(in eventId));
159159

160160
var (traceId, spanId) = Activity.Current is { } activity ?
161161
(activity.TraceId, activity.SpanId) :
@@ -172,25 +172,4 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
172172
stateObj = formatter(state, null);
173173
return stateObj ?? state;
174174
}
175-
176-
internal static LogEventProperty CreateEventIdProperty(EventId eventId)
177-
{
178-
var properties = new List<LogEventProperty>(2);
179-
180-
if (eventId.Id != 0)
181-
{
182-
if (eventId.Id >= 0 && eventId.Id < LowEventIdValues.Length)
183-
// Avoid some allocations
184-
properties.Add(LowEventIdValues[eventId.Id]);
185-
else
186-
properties.Add(new LogEventProperty("Id", new ScalarValue(eventId.Id)));
187-
}
188-
189-
if (eventId.Name != null)
190-
{
191-
properties.Add(new LogEventProperty("Name", new ScalarValue(eventId.Name)));
192-
}
193-
194-
return new LogEventProperty("EventId", new StructureValue(properties));
195-
}
196175
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.Logging;
5+
using Serilog.Events;
6+
using Xunit;
7+
8+
namespace Serilog.Extensions.Logging.Tests
9+
{
10+
public class EventIdPropertyCacheTests
11+
{
12+
[Theory]
13+
[InlineData(1)]
14+
[InlineData(10)]
15+
[InlineData(48)]
16+
[InlineData(100)]
17+
public void LowAndHighNumberedEventIdsAreMapped(int id)
18+
{
19+
// Arrange
20+
var cache = new EventIdPropertyCache(48);
21+
22+
var eventId = new EventId(id, "test");
23+
24+
// Act
25+
var mapped = cache.GetOrCreateProperty(eventId);
26+
27+
// Assert
28+
var value = Assert.IsType<StructureValue>(mapped.Value);
29+
Assert.Equal(2, value.Properties.Count);
30+
31+
var idValue = value.Properties.Single(p => p.Name == "Id").Value;
32+
var scalar = Assert.IsType<ScalarValue>(idValue);
33+
Assert.Equal(id, scalar.Value);
34+
}
35+
}
36+
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -517,22 +517,6 @@ public void Dispose()
517517
}
518518
}
519519

520-
[Theory]
521-
[InlineData(1)]
522-
[InlineData(10)]
523-
[InlineData(48)]
524-
[InlineData(100)]
525-
public void LowAndHighNumberedEventIdsAreMapped(int id)
526-
{
527-
var orig = new EventId(id, "test");
528-
var mapped = SerilogLogger.CreateEventIdProperty(orig);
529-
var value = Assert.IsType<StructureValue>(mapped.Value);
530-
Assert.Equal(2, value.Properties.Count);
531-
var idValue = value.Properties.Single(p => p.Name == "Id").Value;
532-
var scalar = Assert.IsType<ScalarValue>(idValue);
533-
Assert.Equal(id, scalar.Value);
534-
}
535-
536520
[Fact]
537521
public void MismatchedMessageTemplateParameterCountIsHandled()
538522
{

0 commit comments

Comments
 (0)