Skip to content

Commit a2e70ee

Browse files
authored
Merge pull request #166 from nblumhardt/formatter-exceptions
Cache parsed message templates
2 parents 29d4101 + 0507eb3 commit a2e70ee

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
using System;
17+
using Serilog.Events;
18+
using Serilog.Parsing;
19+
using System.Collections;
20+
21+
namespace Serilog.Extensions.Logging
22+
{
23+
class CachingMessageTemplateParser
24+
{
25+
readonly MessageTemplateParser _innerParser = new MessageTemplateParser();
26+
27+
readonly object _templatesLock = new object();
28+
readonly Hashtable _templates = new Hashtable();
29+
30+
const int MaxCacheItems = 1000;
31+
const int MaxCachedTemplateLength = 1024;
32+
33+
public MessageTemplate Parse(string messageTemplate)
34+
{
35+
if (messageTemplate == null) throw new ArgumentNullException(nameof(messageTemplate));
36+
37+
if (messageTemplate.Length > MaxCachedTemplateLength)
38+
return _innerParser.Parse(messageTemplate);
39+
40+
// ReSharper disable once InconsistentlySynchronizedField
41+
// ignored warning because this is by design
42+
var result = (MessageTemplate)_templates[messageTemplate];
43+
if (result != null)
44+
return result;
45+
46+
result = _innerParser.Parse(messageTemplate);
47+
48+
lock (_templatesLock)
49+
{
50+
// Exceeding MaxCacheItems is *not* the sunny day scenario; all we're doing here is preventing out-of-memory
51+
// conditions when the library is used incorrectly. Correct use (templates, rather than
52+
// direct message strings) should barely, if ever, overflow this cache.
53+
54+
// Changing workloads through the lifecycle of an app instance mean we can gain some ground by
55+
// potentially dropping templates generated only in startup, or only during specific infrequent
56+
// activities.
57+
58+
if (_templates.Count == MaxCacheItems)
59+
_templates.Clear();
60+
61+
_templates[messageTemplate] = result;
62+
}
63+
64+
return result;
65+
}
66+
}
67+
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using FrameworkLogger = Microsoft.Extensions.Logging.ILogger;
1111
using System.Reflection;
1212
using Serilog.Debugging;
13-
using Serilog.Parsing;
1413

1514
namespace Serilog.Extensions.Logging
1615
{
@@ -19,7 +18,7 @@ class SerilogLogger : FrameworkLogger
1918
readonly SerilogLoggerProvider _provider;
2019
readonly ILogger _logger;
2120

22-
static readonly MessageTemplateParser MessageTemplateParser = new MessageTemplateParser();
21+
static readonly CachingMessageTemplateParser MessageTemplateParser = new CachingMessageTemplateParser();
2322

2423
// It's rare to see large event ids, as they are category-specific
2524
static readonly LogEventProperty[] LowEventIdValues = Enumerable.Range(0, 48)
@@ -172,4 +171,4 @@ internal static LogEventProperty CreateEventIdProperty(EventId eventId)
172171
return new LogEventProperty("EventId", new StructureValue(properties));
173172
}
174173
}
175-
}
174+
}

0 commit comments

Comments
 (0)