Skip to content

Commit 04a36b0

Browse files
committed
Add limit
1 parent 8bdcc81 commit 04a36b0

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ namespace Serilog.Extensions.Logging;
1313

1414
class SerilogLogger : FrameworkLogger
1515
{
16-
internal static readonly ConcurrentDictionary<string, string> DestructureDictionary = new ConcurrentDictionary<string, string>();
17-
internal static readonly ConcurrentDictionary<string, string> StringifyDictionary = new ConcurrentDictionary<string, string>();
16+
internal static readonly ConcurrentDictionary<string, string> DestructureDictionary = new();
17+
internal static readonly ConcurrentDictionary<string, string> StringifyDictionary = new();
18+
19+
internal static string GetKeyWithoutFirstSymbol(ConcurrentDictionary<string, string> source, string key)
20+
{
21+
if (source.TryGetValue(key, out var value))
22+
return value;
23+
if (source.Count < 1000)
24+
return source.GetOrAdd(key, k => k.Substring(1));
25+
return key.Substring(1);
26+
}
1827

1928
readonly SerilogLoggerProvider _provider;
2029
readonly ILogger _logger;
@@ -92,12 +101,12 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception
92101
}
93102
else if (property.Key.StartsWith("@"))
94103
{
95-
if (logger.BindProperty(DestructureDictionary.GetOrAdd(property.Key, k => k.Substring(1)), property.Value, true, out var destructured))
104+
if (logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
96105
properties.Add(destructured);
97106
}
98107
else if (property.Key.StartsWith("$"))
99108
{
100-
if (logger.BindProperty(StringifyDictionary.GetOrAdd(property.Key, k => k.Substring(1)), property.Value?.ToString(), true, out var stringified))
109+
if (logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
101110
properties.Add(stringified);
102111
}
103112
else

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ void AddProperty(KeyValuePair<string, object> stateProperty)
5959

6060
if (key.StartsWith("@"))
6161
{
62-
key = SerilogLogger.DestructureDictionary.GetOrAdd(key, k => k.Substring(1));
62+
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.DestructureDictionary, key);
6363
destructureObject = true;
6464
}
6565
else if (key.StartsWith("$"))
6666
{
67-
key = SerilogLogger.StringifyDictionary.GetOrAdd(key, k => k.Substring(1));
67+
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.StringifyDictionary, key);
6868
value = value?.ToString();
6969
}
7070

test/Serilog.Extensions.Logging.Benchmarks/LogEventBenchmark.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Serilog Contributors
1+
// Copyright 2019 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,14 +18,16 @@
1818
using Xunit;
1919
using IMelLogger = Microsoft.Extensions.Logging.ILogger;
2020

21+
#pragma warning disable xUnit1013 // Public method should be marked as test
22+
2123
namespace Serilog.Extensions.Logging.Benchmarks
2224
{
2325
[MemoryDiagnoser]
2426
public class LogEventBenchmark
2527
{
2628
private class Person
2729
{
28-
public string Name { get; set; }
30+
public string? Name { get; set; }
2931
public int Age { get; set; }
3032
public override string ToString() => "Fixed text";
3133
}
@@ -36,7 +38,7 @@ private class Person
3638
public LogEventBenchmark()
3739
{
3840
var underlyingLogger = new LoggerConfiguration().CreateLogger();
39-
_melLogger = new SerilogLoggerProvider(underlyingLogger).CreateLogger(GetType().FullName);
41+
_melLogger = new SerilogLoggerProvider(underlyingLogger).CreateLogger(GetType().FullName!);
4042
_bob = new Person { Name = "Bob", Age = 42 };
4143
_alice = new Person { Name = "Alice", Age = 42 };
4244
}

0 commit comments

Comments
 (0)