1
1
using System . Collections . Generic ;
2
+ using System . Collections . ObjectModel ;
3
+ using System . Linq ;
4
+ using FluentAssertions ;
2
5
using Serilog . Core ;
6
+ using Serilog . Events ;
3
7
using Serilog . Sinks . InMemory ;
4
8
using Serilog . Sinks . InMemory . Assertions ;
5
9
using Xunit ;
@@ -29,9 +33,9 @@ public WhenMaskingDestructuredObject()
29
33
public void GivenLogMessageWithDestructuredObjectPropertyThatHasSensitiveData_SensitiveDataIsMasked ( )
30
34
{
31
35
var testObject = new TestObject ( ) ;
32
-
36
+
33
37
_logger . Information ( "Test message {@TestObject}" , testObject ) ;
34
-
38
+
35
39
_sink
36
40
. Should ( )
37
41
. HaveMessage ( "Test message {@TestObject}" )
@@ -47,7 +51,7 @@ public void GivenLogMessageWithDestructuredObjectPropertyThatHasSensitiveData_Se
47
51
public void GivenLogMessageWithDestructuredObjectPropertyThatHasSensitiveDataInNestedProperty_SensitiveDataIsMasked ( )
48
52
{
49
53
var testObject = new TestObject ( ) ;
50
-
54
+
51
55
_logger . Information ( "Test message {@TestObject}" , testObject ) ;
52
56
53
57
_sink
@@ -74,7 +78,7 @@ public void GivenLogMessageWithDestructuredObjectPropertyWithoutSensitiveDataInN
74
78
TestProperty = "also not sensitive"
75
79
}
76
80
} ;
77
-
81
+
78
82
_logger . Information ( "Test message {@TestObject}" , testObject ) ;
79
83
80
84
_sink
@@ -107,17 +111,133 @@ public void GivenConfigurationToMaskSpecificPropertyAndLoggingADestructuredObjec
107
111
. WithProperty ( "SensitiveProperty" )
108
112
. WithValue ( "***MASKED***" ) ;
109
113
}
114
+
115
+ [ Fact ]
116
+ public void GivenDestructuredObjectHasListOfNestedObjects ( )
117
+ {
118
+ var testObject = new TestObject ( ) ;
119
+
120
+ _logger . Information ( "Test message {@TestObject}" , testObject ) ;
121
+
122
+ var elements = _sink
123
+ . Should ( )
124
+ . HaveMessage ( "Test message {@TestObject}" )
125
+ . Appearing ( )
126
+ . Once ( )
127
+ . WithProperty ( "TestObject" )
128
+ . HavingADestructuredObject ( )
129
+ . WithProperty ( "NestedList" )
130
+ . Subject
131
+ . As < SequenceValue > ( )
132
+ . Elements ;
133
+
134
+ foreach ( var structureValue in elements . OfType < StructureValue > ( ) )
135
+ {
136
+ var testProperty = structureValue . Properties . Single ( p => p . Name == "TestProperty" ) ;
137
+ testProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
138
+ var sensitiveProperty = structureValue . Properties . Single ( p => p . Name == "SensitiveProperty" ) ;
139
+ sensitiveProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
140
+ }
141
+ }
142
+
143
+ [ Fact ]
144
+ public void GivenDestructuredObjectHasArrayOfNestedObjects ( )
145
+ {
146
+ var testObject = new TestObject ( ) ;
147
+
148
+ _logger . Information ( "Test message {@TestObject}" , testObject ) ;
149
+
150
+ var elements = _sink
151
+ . Should ( )
152
+ . HaveMessage ( "Test message {@TestObject}" )
153
+ . Appearing ( )
154
+ . Once ( )
155
+ . WithProperty ( "TestObject" )
156
+ . HavingADestructuredObject ( )
157
+ . WithProperty ( "NestedArray" )
158
+ . Subject
159
+ . As < SequenceValue > ( )
160
+ . Elements ;
161
+
162
+ foreach ( var structureValue in elements . OfType < StructureValue > ( ) )
163
+ {
164
+ var testProperty = structureValue . Properties . Single ( p => p . Name == "TestProperty" ) ;
165
+ testProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
166
+ var sensitiveProperty = structureValue . Properties . Single ( p => p . Name == "SensitiveProperty" ) ;
167
+ sensitiveProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
168
+ }
169
+ }
170
+
171
+ [ Fact ]
172
+ public void GivenDestructuredObjectHasCollectionOfNestedObjects ( )
173
+ {
174
+ var testObject = new TestObject ( ) ;
175
+
176
+ _logger . Information ( "Test message {@TestObject}" , testObject ) ;
177
+
178
+ var elements = _sink
179
+ . Should ( )
180
+ . HaveMessage ( "Test message {@TestObject}" )
181
+ . Appearing ( )
182
+ . Once ( )
183
+ . WithProperty ( "TestObject" )
184
+ . HavingADestructuredObject ( )
185
+ . WithProperty ( "NestedCollection" )
186
+ . Subject
187
+ . As < SequenceValue > ( )
188
+ . Elements ;
189
+
190
+ foreach ( var structureValue in elements . OfType < StructureValue > ( ) )
191
+ {
192
+ var testProperty = structureValue . Properties . Single ( p => p . Name == "TestProperty" ) ;
193
+ testProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
194
+ var sensitiveProperty = structureValue . Properties . Single ( p => p . Name == "SensitiveProperty" ) ;
195
+ sensitiveProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
196
+ }
197
+ }
198
+
199
+ [ Fact ]
200
+ public void GivenDestructuredObjectIsCollectionOfObjects ( )
201
+ {
202
+ var collection = new [ ] { new TestObject ( ) , new TestObject ( ) } ;
203
+
204
+ _logger . Information ( "Test message {@Collection}" , collection ) ;
205
+
206
+ var elements = _sink
207
+ . Should ( )
208
+ . HaveMessage ( "Test message {@Collection}" )
209
+ . Appearing ( )
210
+ . Once ( )
211
+ . WithProperty ( "Collection" )
212
+ . Subject
213
+ . As < SequenceValue > ( )
214
+ . Elements ;
215
+
216
+ foreach ( var structureValue in elements . OfType < StructureValue > ( ) )
217
+ {
218
+ var testProperty = structureValue . Properties . Single ( p => p . Name == "TestProperty" ) ;
219
+ testProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
220
+ var sensitiveProperty = structureValue . Properties . Single ( p => p . Name == "SensitiveProperty" ) ;
221
+ sensitiveProperty . Value . ToString ( ) . Should ( ) . Be ( "\" ***MASKED***\" " ) ;
222
+ }
223
+ }
110
224
}
111
225
112
226
public class TestObject
113
227
{
114
228
public string TestProperty { get ; set ; } = "[email protected] " ;
115
229
public string SensitiveProperty { get ; set ; } = "Super sensitive data" ;
116
230
public NestedTestObject Nested { get ; set ; } = new NestedTestObject ( ) ;
231
+
232
+ public List < NestedTestObject > NestedList { get ; set ; } = new ( ) { new NestedTestObject ( ) , new NestedTestObject ( ) } ;
233
+
234
+ public NestedTestObject [ ] NestedArray { get ; set ; } = { new NestedTestObject ( ) , new NestedTestObject ( ) } ;
235
+ public Collection < NestedTestObject > NestedCollection { get ; set ; } = new ( ) { new NestedTestObject ( ) , new NestedTestObject ( ) } ;
117
236
}
118
237
119
238
public class NestedTestObject
120
239
{
121
240
public string TestProperty { get ; set ; } = "[email protected] " ;
241
+ public string SensitiveProperty { get ; set ; } = "Super sensitive data" ;
122
242
}
123
243
}
0 commit comments