Skip to content

Commit 1ffaa7b

Browse files
committed
Use FormattedLogValues as the event state
1 parent e94a240 commit 1ffaa7b

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollectionSink.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Linq;
1617
using Microsoft.Extensions.Logging;
18+
using Microsoft.Extensions.Logging.Internal;
1719
using Serilog.Core;
1820
using Serilog.Events;
21+
using Serilog.Parsing;
1922

2023
namespace Serilog.Extensions.Logging
2124
{
@@ -39,17 +42,31 @@ sourceContextProperty is ScalarValue sourceContextValue &&
3942
categoryName = sourceContext;
4043
}
4144

45+
// Allocates like mad, but first make it work, then make it work fast ;-)
46+
var flv = new FormattedLogValues(
47+
logEvent.MessageTemplate.Text,
48+
logEvent.MessageTemplate.Tokens
49+
.OfType<PropertyToken>()
50+
.Select(p =>
51+
{
52+
if (!logEvent.Properties.TryGetValue(p.PropertyName, out var value))
53+
return null;
54+
if (value is ScalarValue sv)
55+
return sv.Value;
56+
return value;
57+
})
58+
.ToArray());
59+
4260
foreach (var provider in _providers.Providers)
4361
{
4462
var logger = provider.CreateLogger(categoryName);
4563

46-
4764
logger.Log(
4865
LevelMapping.ToExtensionsLevel(logEvent.Level),
4966
default(EventId),
50-
logEvent,
67+
flv,
5168
logEvent.Exception,
52-
(s, ex) => s.RenderMessage());
69+
(s, e) => s.ToString());
5370
}
5471
}
5572

0 commit comments

Comments
 (0)