Skip to content

Commit f223c27

Browse files
committed
Lazy coercion of levels/exceptions
1 parent a63c347 commit f223c27

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

example/Sample/Program.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
using Serilog;
1+
using System;
2+
using Serilog;
3+
using Serilog.Debugging;
4+
using Serilog.Templates;
25

36
namespace Sample
47
{
58
public class Program
69
{
710
public static void Main()
811
{
12+
SelfLog.Enable(Console.Error);
13+
914
const string expr = "@Level = 'Information' and AppId is not null and Items[?] like 'C%'";
1015

1116
using var log = new LoggerConfiguration()
@@ -15,7 +20,9 @@ public static void Main()
1520
.Filter.ByIncludingOnly(expr)
1621
.WriteTo.Console(outputTemplate:
1722
"[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] {Message:lj} (first item is {FirstItem}){NewLine}{Exception}")
18-
.CreateLogger();
23+
.WriteTo.Console(new OutputTemplate(
24+
"[{@Timestamp} {@Level} ({SourceContext})] {@Message} (first item is {Items[0]})\n{@Exception}"))
25+
.CreateLogger();
1926

2027
log.ForContext<Program>().Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
2128
log.Warning("Cart contains {@Items}", new[] { "Tea", "Coffee" });

src/Serilog.Expressions/Expressions/Compilation/Linq/LinqExpressionCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ protected override Expression<CompiledExpression> Transform(AmbientPropertyExpre
178178
if (px.IsBuiltIn)
179179
{
180180
if (px.PropertyName == BuiltInProperty.Level)
181-
return context => new ScalarValue(context.Level.ToString());
181+
return context => new ScalarValue(context.Level);
182182

183183
if (px.PropertyName == BuiltInProperty.Message)
184184
return context => NormalizeBuiltInProperty(context.RenderMessage(null));
185185

186186
if (px.PropertyName == BuiltInProperty.Exception)
187-
return context => NormalizeBuiltInProperty(context.Exception == null ? null : context.Exception.ToString());
187+
return context => context.Exception == null ? null : new ScalarValue(context.Exception);
188188

189189
if (px.PropertyName == BuiltInProperty.Timestamp)
190190
return context => new ScalarValue(context.Timestamp);

src/Serilog.Expressions/Expressions/Runtime/Coerce.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,25 @@ public static bool True(LogEventPropertyValue value)
4545

4646
public static bool String(LogEventPropertyValue value, out string str)
4747
{
48-
if (value is ScalarValue sv &&
49-
sv.Value is string s)
48+
if (value is ScalarValue sv)
5049
{
51-
str = s;
52-
return true;
50+
if (sv.Value is string s)
51+
{
52+
str = s;
53+
return true;
54+
}
55+
56+
if (sv.Value is Exception ex)
57+
{
58+
str = ex.ToString();
59+
return true;
60+
}
61+
62+
if (sv.Value?.GetType().IsEnum ?? false)
63+
{
64+
str = sv.Value.ToString();
65+
return true;
66+
}
5367
}
5468

5569
str = default;

src/Serilog.Expressions/Expressions/Runtime/RuntimeOperators.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ static bool UnboxedEqualHelper(LogEventPropertyValue left, LogEventPropertyValue
137137
Coerce.Numeric(right, out var r))
138138
return l == r;
139139

140+
if (Coerce.String(left, out var ls) &&
141+
Coerce.String(right, out var rs))
142+
return ls == rs;
143+
140144
if (left is ScalarValue sl &&
141145
right is ScalarValue sr)
142146
return sl.Value?.Equals(sr.Value) ?? sr.Value == null;

src/Serilog.Expressions/Templates/Compilation/CompiledFormattedExpression.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
using System.IO;
33
using Serilog.Events;
44
using Serilog.Expressions;
5+
using Serilog.Formatting.Json;
56

67
namespace Serilog.Templates.Compilation
78
{
89
class CompiledFormattedExpression : CompiledTemplate
910
{
11+
static readonly JsonValueFormatter JsonFormatter = new JsonValueFormatter("$type");
12+
1013
readonly CompiledExpression _expression;
1114
readonly string _format;
1215

@@ -19,7 +22,23 @@ public CompiledFormattedExpression(CompiledExpression expression, string format)
1922
public override void Evaluate(LogEvent logEvent, TextWriter output, IFormatProvider formatProvider)
2023
{
2124
var value = _expression(logEvent);
22-
value?.Render(output, _format, formatProvider);
25+
if (value == null)
26+
return; // Undefined is empty
27+
28+
if (value is ScalarValue scalar)
29+
{
30+
if (scalar.Value is null)
31+
return; // Null is empty
32+
33+
if (scalar.Value is IFormattable fmt)
34+
output.Write(fmt.ToString(_format, formatProvider));
35+
else
36+
output.Write(scalar.Value.ToString());
37+
}
38+
else
39+
{
40+
JsonFormatter.Format(value, output);
41+
}
2342
}
2443
}
2544
}

0 commit comments

Comments
 (0)