3
3
4
4
using System ;
5
5
using System . Collections ;
6
+ using System . Collections . Concurrent ;
6
7
using System . Collections . Generic ;
7
8
8
9
namespace Microsoft . Extensions . Logging . Internal
@@ -14,16 +15,27 @@ namespace Microsoft.Extensions.Logging.Internal
14
15
public class FormattedLogValues : IReadOnlyList < KeyValuePair < string , object > >
15
16
{
16
17
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 > ( ) ;
18
19
private readonly LogValuesFormatter _formatter ;
19
20
private readonly object [ ] _values ;
20
21
private readonly string _originalMessage ;
22
+ private const int MaxCachedFormatters = 1024 ;
21
23
22
24
public FormattedLogValues ( string format , params object [ ] values )
23
25
{
24
26
if ( values ? . Length != 0 && format != null )
25
27
{
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
+ }
27
39
}
28
40
29
41
_originalMessage = format ?? NullFormat ;
0 commit comments