Skip to content

Commit 6afbf9e

Browse files
committed
More scalar value conversion
1 parent 2b3de4d commit 6afbf9e

27 files changed

+453
-616
lines changed
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
using System;
22
using System.Globalization;
3+
using Serilog.Events;
34

45
namespace Serilog.Expressions.Ast
56
{
67
class ConstantExpression : Expression
78
{
8-
public ConstantExpression(object constantValue)
9+
public ConstantExpression(LogEventPropertyValue constant)
910
{
10-
ConstantValue = constantValue;
11+
Constant = constant ?? throw new ArgumentNullException(nameof(constant));
1112
}
1213

13-
public object ConstantValue { get; }
14+
public LogEventPropertyValue Constant { get; }
1415

1516
public override string ToString()
1617
{
17-
switch (ConstantValue)
18+
if (Constant is ScalarValue sv)
1819
{
19-
case string s:
20-
return "'" + s.Replace("'", "''") + "'";
21-
case true:
22-
return "true";
23-
case false:
24-
return "false";
25-
case IFormattable formattable:
26-
return formattable.ToString(null, CultureInfo.InvariantCulture);
27-
default:
28-
return (ConstantValue ?? "null").ToString();
20+
switch (sv.Value)
21+
{
22+
case string s:
23+
return "'" + s.Replace("'", "''") + "'";
24+
case true:
25+
return "true";
26+
case false:
27+
return "false";
28+
case IFormattable formattable:
29+
return formattable.ToString(null, CultureInfo.InvariantCulture);
30+
default:
31+
return (sv.Value ?? "null").ToString();
32+
}
2933
}
34+
35+
return Constant.ToString();
3036
}
3137
}
3238
}

src/Serilog.Expressions/Expressions/Compilation/Arrays/ConstantArrayEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected override Expression Transform(ArrayExpression ax)
2323
return new ConstantExpression(
2424
new SequenceValue(ax.Elements
2525
.Cast<ConstantExpression>()
26-
.Select(ce => Representation.Recapture(ce.ConstantValue))));
26+
.Select(ce => ce.Constant)));
2727
}
2828

2929
return base.Transform(ax);

src/Serilog.Expressions/Expressions/Compilation/CompiledExpression.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Serilog.Expressions/Expressions/Compilation/ExpressionCompiler.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
using Serilog.Expressions.Compilation.Linq;
99
using Serilog.Expressions.Compilation.Properties;
1010
using Serilog.Expressions.Compilation.Wildcards;
11-
using Serilog.Expressions.Runtime;
1211

1312
namespace Serilog.Expressions.Compilation
1413
{
1514
static class ExpressionCompiler
1615
{
17-
public static Func<LogEvent, object> CompileAndExpose(Expression expression)
16+
public static CompiledExpression Compile(Expression expression)
1817
{
1918
var actual = expression;
2019
actual = PropertiesObjectAccessorTransformer.Rewrite(actual);
@@ -24,12 +23,7 @@ public static Func<LogEvent, object> CompileAndExpose(Expression expression)
2423
actual = IsOperatorTransformer.Rewrite(actual);
2524
actual = EvaluationCostReordering.Reorder(actual);
2625

27-
var compiled = LinqExpressionCompiler.Compile(actual);
28-
return ctx =>
29-
{
30-
var result = compiled(ctx);
31-
return Representation.Expose(result);
32-
};
26+
return LinqExpressionCompiler.Compile(actual);
3327
}
3428
}
3529
}

src/Serilog.Expressions/Expressions/Compilation/Is/IsOperatorTransformer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ protected override Expression Transform(CallExpression lx)
1717

1818
if (lx.Operands[1] is ConstantExpression nul)
1919
{
20-
return nul.ConstantValue != null ? base.Transform(lx) : new CallExpression(Operators.RuntimeOpIsNull, lx.Operands[0]);
20+
return nul.Constant != null ? base.Transform(lx) : new CallExpression(Operators.RuntimeOpIsNull, lx.Operands[0]);
2121
}
2222

2323
if (!(lx.Operands[1] is CallExpression not) || not.Operands.Length != 1)
2424
return base.Transform(lx);
2525

2626
nul = not.Operands[0] as ConstantExpression;
27-
if (nul == null || nul.ConstantValue != null)
27+
if (nul == null || nul.Constant != null)
2828
return base.Transform(lx);
2929

3030
return new CallExpression(Operators.RuntimeOpIsNotNull, lx.Operands[0]);

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

Lines changed: 39 additions & 73 deletions
Large diffs are not rendered by default.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Serilog.Events;
2+
using Serilog.Expressions.Ast;
3+
4+
namespace Serilog.Expressions.Compilation
5+
{
6+
static class Pattern
7+
{
8+
public static bool IsAmbientProperty(Expression expression, string name, bool isBuiltIn)
9+
{
10+
return expression is AmbientPropertyExpression px &&
11+
px.PropertyName == name &&
12+
px.IsBuiltIn == isBuiltIn;
13+
}
14+
15+
public static bool IsStringConstant(Expression expression, out string value)
16+
{
17+
if (expression is ConstantExpression cx &&
18+
cx.Constant is ScalarValue sv &&
19+
sv.Value is string s)
20+
{
21+
value = s;
22+
return true;
23+
}
24+
25+
value = null;
26+
return false;
27+
}
28+
}
29+
}

src/Serilog.Expressions/Expressions/Compilation/Properties/PropertiesObjectAccessorTransformer.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ public static Expression Rewrite(Expression actual)
1010
return new PropertiesObjectAccessorTransformer().Transform(actual);
1111
}
1212

13-
protected override Expression Transform(CallExpression lx)
13+
protected override Expression Transform(AccessorExpression ax)
1414
{
15-
if (!Operators.SameOperator(lx.OperatorName, Operators.OpElementAt) || lx.Operands.Length != 2)
16-
return base.Transform(lx);
17-
18-
if (!(lx.Operands[0] is AmbientPropertyExpression p)
19-
|| !(lx.Operands[1] is ConstantExpression n)
20-
|| !p.IsBuiltIn
21-
|| p.PropertyName != "Properties"
22-
|| !(n.ConstantValue is string name))
23-
return base.Transform(lx);
15+
if (!Pattern.IsAmbientProperty(ax.Receiver, "Properties", true))
16+
return base.Transform(ax);
17+
18+
return new AmbientPropertyExpression(ax.MemberName, false);
19+
}
2420

21+
protected override Expression Transform(IndexerExpression ix)
22+
{
23+
if (!Pattern.IsAmbientProperty(ix.Receiver, "Properties", true) ||
24+
!Pattern.IsStringConstant(ix.Index, out var name))
25+
return base.Transform(ix);
26+
2527
return new AmbientPropertyExpression(name, false);
2628
}
2729
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Serilog.Events;
2+
3+
#nullable enable
4+
5+
namespace Serilog.Expressions
6+
{
7+
/// <summary>
8+
/// A compiled expression evaluated against a <see cref="LogEvent"/>.
9+
/// </summary>
10+
/// <param name="logEvent"></param>
11+
/// <returns>The result of evaluating the expression, represented as a <see cref="LogEventPropertyValue"/>,
12+
/// or <c langword="null">null</c> if the result is undefined.</returns>
13+
public delegate LogEventPropertyValue? CompiledExpression(LogEvent logEvent);
14+
}

src/Serilog.Expressions/Expressions/LoggingFilterSwitch.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2015 Serilog Contributors
1+
// Copyright Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ public class LoggingFilterSwitch : ILogEventFilter
2727
// no attempt to synchronize Expression, ToString(), and IsIncluded(),
2828
// for any observer, this at least ensures they won't be permanently out-of-sync for
2929
// all observers.
30-
volatile Tuple<string, Func<LogEvent, object>> _filter;
30+
volatile Tuple<string, CompiledExpression> _filter;
3131

3232
/// <summary>
3333
/// Construct a <see cref="LoggingFilterSwitch"/>, optionally initialized
@@ -50,6 +50,7 @@ public LoggingFilterSwitch(string expression = null)
5050
/// </summary>
5151
public string Expression
5252
{
53+
// ReSharper disable once UnusedMember.Global
5354
get
5455
{
5556
var filter = _filter;
@@ -63,7 +64,7 @@ public string Expression
6364
}
6465
else
6566
{
66-
_filter = new Tuple<string, Func<LogEvent, object>>(
67+
_filter = new Tuple<string, CompiledExpression>(
6768
value,
6869
SerilogExpression.Compile(value));
6970
}
@@ -80,7 +81,7 @@ public bool IsEnabled(LogEvent logEvent)
8081
if (filter == null)
8182
return true;
8283

83-
return true.Equals(filter.Item2(logEvent));
84+
return true.Equals((filter.Item2(logEvent) as ScalarValue)?.Value);
8485
}
8586

8687
/// <inheritdoc/>

0 commit comments

Comments
 (0)