@@ -22,22 +22,22 @@ class CompiledRepetition : CompiledTemplate
22
22
{
23
23
readonly Evaluatable _enumerable ;
24
24
readonly string ? _keyOrElementName ;
25
- readonly string ? _valueName ;
25
+ readonly string ? _valueOrIndexName ;
26
26
readonly CompiledTemplate _body ;
27
27
readonly CompiledTemplate ? _delimiter ;
28
28
readonly CompiledTemplate ? _alternative ;
29
29
30
30
public CompiledRepetition (
31
31
Evaluatable enumerable ,
32
32
string ? keyOrElementName ,
33
- string ? valueName ,
33
+ string ? valueOrIndexName ,
34
34
CompiledTemplate body ,
35
35
CompiledTemplate ? delimiter ,
36
36
CompiledTemplate ? alternative )
37
37
{
38
38
_enumerable = enumerable ;
39
39
_keyOrElementName = keyOrElementName ;
40
- _valueName = valueName ;
40
+ _valueOrIndexName = valueOrIndexName ;
41
41
_body = body ;
42
42
_delimiter = delimiter ;
43
43
_alternative = alternative ;
@@ -52,29 +52,32 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
52
52
return ;
53
53
}
54
54
55
- if ( enumerable is SequenceValue sv )
55
+ if ( enumerable is SequenceValue sequence )
56
56
{
57
- if ( sv . Elements . Count == 0 )
57
+ if ( sequence . Elements . Count == 0 )
58
58
{
59
59
_alternative ? . Evaluate ( ctx , output ) ;
60
60
return ;
61
61
}
62
62
63
- var first = true ;
64
- foreach ( var element in sv . Elements )
63
+ for ( var i = 0 ; i < sequence . Elements . Count ; ++ i )
65
64
{
66
- if ( element == null )
67
- continue ; // Should have been invalid but Serilog didn't check and so this does occur in the wild.
65
+ // Null elements should have been invalid but Serilog didn't check, and so this does occur in the wild.
66
+ var element = sequence . Elements [ i ] ?? new ScalarValue ( null ) ;
68
67
69
- if ( first )
70
- first = false ;
71
- else
68
+ if ( i != 0 )
69
+ {
72
70
_delimiter ? . Evaluate ( ctx , output ) ;
71
+ }
73
72
74
73
var local = _keyOrElementName != null
75
- ? new ( ctx . LogEvent , Locals . Set ( ctx . Locals , _keyOrElementName , element ) )
74
+ ? new EvaluationContext ( ctx . LogEvent , Locals . Set ( ctx . Locals , _keyOrElementName , element ) )
76
75
: ctx ;
77
76
77
+ local = _valueOrIndexName != null
78
+ ? new EvaluationContext ( local . LogEvent , Locals . Set ( local . Locals , _valueOrIndexName , new ScalarValue ( i ) ) )
79
+ : local ;
80
+
78
81
_body . Evaluate ( local , output ) ;
79
82
}
80
83
@@ -101,8 +104,8 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
101
104
? new ( ctx . LogEvent , Locals . Set ( ctx . Locals , _keyOrElementName , new ScalarValue ( member . Name ) ) )
102
105
: ctx ;
103
106
104
- local = _valueName != null
105
- ? new ( local . LogEvent , Locals . Set ( local . Locals , _valueName , member . Value ) )
107
+ local = _valueOrIndexName != null
108
+ ? new ( local . LogEvent , Locals . Set ( local . Locals , _valueOrIndexName , member . Value ) )
106
109
: local ;
107
110
108
111
_body . Evaluate ( local , output ) ;
@@ -129,8 +132,8 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
129
132
? new ( ctx . LogEvent , Locals . Set ( ctx . Locals , _keyOrElementName , element . Key ) )
130
133
: ctx ;
131
134
132
- local = _valueName != null
133
- ? new ( local . LogEvent , Locals . Set ( local . Locals , _valueName , element . Value ) )
135
+ local = _valueOrIndexName != null
136
+ ? new ( local . LogEvent , Locals . Set ( local . Locals , _valueOrIndexName , element . Value ) )
134
137
: local ;
135
138
136
139
_body . Evaluate ( local , output ) ;
0 commit comments