Skip to content

Commit 1c93844

Browse files
committed
Sort out more numerics
1 parent 6afbf9e commit 1c93844

File tree

8 files changed

+43
-14
lines changed

8 files changed

+43
-14
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ protected override Expression<CompiledExpression> Transform(CallExpression lx)
4949

5050
var statements = new List<System.Linq.Expressions.Expression>();
5151
var first = true;
52+
var shortCircuit = false;
53+
System.Linq.Expressions.Expression shortCircuitElse = null;
5254
foreach (var op in operandValues)
5355
{
56+
if (shortCircuit)
57+
{
58+
shortCircuitElse = op;
59+
break;
60+
}
61+
5462
var opam = System.Linq.Expressions.Expression.Variable(typeof(LogEventPropertyValue));
5563
operandVars.Add(opam);
5664
statements.Add(System.Linq.Expressions.Expression.Assign(opam, op));
@@ -60,19 +68,21 @@ protected override Expression<CompiledExpression> Transform(CallExpression lx)
6068
Expression<Func<LogEventPropertyValue, bool>> shortCircuitIf = v => !(v is ScalarValue) || !true.Equals(((ScalarValue)v).Value);
6169
var scc = Splice(shortCircuitIf, opam);
6270
statements.Add(System.Linq.Expressions.Expression.IfThen(scc, System.Linq.Expressions.Expression.Return(rtn, System.Linq.Expressions.Expression.Constant(new ScalarValue(false), typeof(LogEventPropertyValue)))));
71+
shortCircuit = true;
6372
}
6473

6574
if (first && Operators.SameOperator(lx.OperatorName, Operators.OpOr))
6675
{
6776
Expression<Func<LogEventPropertyValue, bool>> shortCircuitIf = v => v is ScalarValue && true.Equals(((ScalarValue)v).Value);
6877
var scc = Splice(shortCircuitIf, opam);
6978
statements.Add(System.Linq.Expressions.Expression.IfThen(scc, System.Linq.Expressions.Expression.Return(rtn, System.Linq.Expressions.Expression.Constant(new ScalarValue(true), typeof(LogEventPropertyValue)))));
79+
shortCircuit = true;
7080
}
7181

7282
first = false;
7383
}
7484

75-
statements.Add(System.Linq.Expressions.Expression.Return(rtn, System.Linq.Expressions.Expression.Call(m, operandVars)));
85+
statements.Add(System.Linq.Expressions.Expression.Return(rtn, shortCircuitElse ?? System.Linq.Expressions.Expression.Call(m, operandVars)));
7686
statements.Add(System.Linq.Expressions.Expression.Label(rtn, System.Linq.Expressions.Expression.Constant(null, typeof(LogEventPropertyValue))));
7787

7888
return System.Linq.Expressions.Expression.Lambda<CompiledExpression>(

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ namespace Serilog.Expressions.Runtime
66
{
77
static class Coerce
88
{
9-
static readonly Type[] NumericTypes = { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort),
10-
typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double) };
9+
static readonly Type[] NumericTypes = { typeof(decimal),
10+
typeof(int), typeof(long), typeof(double),
11+
typeof(float), typeof(uint), typeof(sbyte),
12+
typeof(byte), typeof(short), typeof(ushort), typeof(ulong) };
1113

1214
public static bool Numeric(LogEventPropertyValue value, out decimal numeric)
1315
{
1416
if (value is ScalarValue sv &&
1517
sv.Value != null &&
16-
NumericTypes.Contains(sv.GetType()))
18+
NumericTypes.Contains(sv.Value.GetType()))
1719
{
1820
numeric = (decimal)Convert.ChangeType(sv.Value, typeof(decimal));
1921
return true;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public static LogEventPropertyValue GreaterThanOrEqual(LogEventPropertyValue lef
122122

123123
public static LogEventPropertyValue Equal(LogEventPropertyValue left, LogEventPropertyValue right)
124124
{
125+
if (Coerce.Numeric(left, out var l) &&
126+
Coerce.Numeric(right, out var r))
127+
return ScalarBoolean(l == r);
128+
125129
return ScalarBoolean(UnboxedEqualHelper(left, right));
126130
}
127131

src/Serilog.Expressions/LoggerEnrichmentConfigurationExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using Serilog.Configuration;
1717
using Serilog.Expressions;
18+
using Serilog.Expressions.Runtime;
1819

1920
namespace Serilog
2021
{
@@ -42,7 +43,7 @@ public static LoggerConfiguration When(
4243
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
4344

4445
var compiled = SerilogExpression.Compile(expression);
45-
return loggerEnrichmentConfiguration.When(e => true.Equals(compiled(e)), configureEnricher);
46+
return loggerEnrichmentConfiguration.When(e => Coerce.True(compiled(e)), configureEnricher);
4647
}
4748
}
4849
}

src/Serilog.Expressions/LoggerFilterConfigurationExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Serilog.Configuration;
33
using Serilog.Expressions;
4+
using Serilog.Expressions.Runtime;
45

56
namespace Serilog
67
{
@@ -21,7 +22,7 @@ public static LoggerConfiguration ByIncludingOnly(this LoggerFilterConfiguration
2122
if (expression == null) throw new ArgumentNullException(nameof(expression));
2223

2324
var compiled = SerilogExpression.Compile(expression);
24-
return loggerFilterConfiguration.ByIncludingOnly(e => true.Equals(compiled(e)));
25+
return loggerFilterConfiguration.ByIncludingOnly(e => Coerce.True(compiled(e)));
2526
}
2627

2728
/// <summary>
@@ -36,7 +37,7 @@ public static LoggerConfiguration ByExcluding(this LoggerFilterConfiguration log
3637
if (expression == null) throw new ArgumentNullException(nameof(expression));
3738

3839
var compiled = SerilogExpression.Compile(expression);
39-
return loggerFilterConfiguration.ByExcluding(e => true.Equals(compiled(e)));
40+
return loggerFilterConfiguration.ByExcluding(e => Coerce.True(compiled(e)));
4041
}
4142

4243
/// <summary>

src/Serilog.Expressions/LoggerSinkConfigurationExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using Serilog.Configuration;
1717
using Serilog.Expressions;
18+
using Serilog.Expressions.Runtime;
1819

1920
namespace Serilog
2021
{
@@ -43,7 +44,7 @@ public static LoggerConfiguration Conditional(
4344
if (configureSink == null) throw new ArgumentNullException(nameof(configureSink));
4445

4546
var compiled = SerilogExpression.Compile(expression);
46-
return loggerSinkConfiguration.Conditional(e => true.Equals(compiled(e)), configureSink);
47+
return loggerSinkConfiguration.Conditional(e => Coerce.True(compiled(e)), configureSink);
4748
}
4849
}
4950
}

test/Serilog.Expressions.Tests/ExpressionCompilerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public void InMatchesLiterals()
129129
public void InExaminesSequenceValues()
130130
{
131131
AssertEvaluation("5 not in Numbers",
132-
Some.InformationEvent("{Numbers}", new object[] {new []{1, 2, 3}}),
133-
Some.InformationEvent("{Numbers}", new object[] { new [] { 1, 5, 3 }}),
132+
Some.InformationEvent("{Numbers}", new []{1, 2, 3}),
133+
Some.InformationEvent("{Numbers}", new [] { 1, 5, 3 }),
134134
Some.InformationEvent());
135135
}
136136

test/Serilog.Expressions.Tests/ExpressionEvaluationTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using Serilog.Events;
56
using Serilog.Expressions.Runtime;
67
using Serilog.Expressions.Tests.Support;
78
using Xunit;
@@ -29,17 +30,26 @@ static IEnumerable<object[]> ReadCases(string filename)
2930
[MemberData(nameof(EphemeralExpressionEvaluationCases))]
3031
public void EphemeralExpressionsAreCorrectlyEvaluated(string expr, string result)
3132
{
32-
var actual = SerilogExpression.Compile(expr)(Some.InformationEvent());
33-
var expected = SerilogExpression.Compile(result)(Some.InformationEvent());
33+
var evt = Some.InformationEvent();
34+
var actual = SerilogExpression.Compile(expr)(evt);
35+
var expected = SerilogExpression.Compile(result)(evt);
3436

3537
if (expected is null)
3638
{
37-
Assert.True(actual is null, $"Expected: undefined{Environment.NewLine}Actual: {actual}");
39+
Assert.True(actual is null, $"Expected value: undefined{Environment.NewLine}Actual value: {Display(actual)}");
3840
}
3941
else
4042
{
41-
Assert.True(RuntimeOperators.Equal(actual, expected)?.Equals(true), $"Expected: {expected}{Environment.NewLine}Actual: {actual}");
43+
Assert.True(Coerce.True(RuntimeOperators.Equal(actual, expected)), $"Expected value: {Display(expected)}{Environment.NewLine}Actual value: {Display(actual)}");
4244
}
4345
}
46+
47+
static string Display(LogEventPropertyValue value)
48+
{
49+
if (value == null)
50+
return "undefined";
51+
52+
return value.ToString();
53+
}
4454
}
4555
}

0 commit comments

Comments
 (0)