Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 5e9d398

Browse files
committed
Option 2: Just cap
1 parent 5ba5fd4 commit 5e9d398

File tree

2 files changed

+14
-239
lines changed

2 files changed

+14
-239
lines changed

src/Microsoft.Extensions.Logging.Abstractions/Internal/ConcurrentLruCache.cs

-237
This file was deleted.

src/Microsoft.Extensions.Logging.Abstractions/Internal/FormattedLogValues.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections;
6+
using System.Collections.Concurrent;
67
using System.Collections.Generic;
78

89
namespace Microsoft.Extensions.Logging.Internal
@@ -14,16 +15,27 @@ namespace Microsoft.Extensions.Logging.Internal
1415
public class FormattedLogValues : IReadOnlyList<KeyValuePair<string, object>>
1516
{
1617
private const string NullFormat = "[null]";
17-
private static ConcurrentLruCache<string, LogValuesFormatter> _formatters = new ConcurrentLruCache<string, LogValuesFormatter>(1024);
18+
private static ConcurrentDictionary<string, LogValuesFormatter> _formatters = new ConcurrentDictionary<string, LogValuesFormatter>();
1819
private readonly LogValuesFormatter _formatter;
1920
private readonly object[] _values;
2021
private readonly string _originalMessage;
22+
private const int MaxCachedFormatters = 1024;
2123

2224
public FormattedLogValues(string format, params object[] values)
2325
{
2426
if (values?.Length != 0 && format != null)
2527
{
26-
_formatter = _formatters.GetOrAdd(format, format, f => new LogValuesFormatter(f));
28+
if (_formatters.Count >= MaxCachedFormatters)
29+
{
30+
if (!_formatters.TryGetValue(format, out _formatter))
31+
{
32+
_formatter = new LogValuesFormatter(format);
33+
}
34+
}
35+
else
36+
{
37+
_formatter = _formatters.GetOrAdd(format, f => new LogValuesFormatter(f));
38+
}
2739
}
2840

2941
_originalMessage = format ?? NullFormat;

0 commit comments

Comments
 (0)