@@ -288,6 +288,82 @@ public void WhenDisposeIsTrueProvidedLoggerIsDisposed()
288
288
Assert . True ( logger . IsDisposed ) ;
289
289
}
290
290
291
+ [ Fact ]
292
+ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate ( )
293
+ {
294
+ var t = SetUp ( LogLevel . Trace ) ;
295
+ var logger = t . Item1 ;
296
+ var sink = t . Item2 ;
297
+
298
+ using ( logger . BeginScope ( "{@Person}" , new Person { FirstName = "John" , LastName = "Smith" } ) )
299
+ {
300
+ logger . Log ( LogLevel . Information , 0 , TestMessage , null , null ) ;
301
+ }
302
+
303
+ Assert . Equal ( 1 , sink . Writes . Count ) ;
304
+ Assert . True ( sink . Writes [ 0 ] . Properties . ContainsKey ( "Person" ) ) ;
305
+
306
+ var person = ( StructureValue ) sink . Writes [ 0 ] . Properties [ "Person" ] ;
307
+ var firstName = ( ScalarValue ) person . Properties . Single ( p => p . Name == "FirstName" ) . Value ;
308
+ var lastName = ( ScalarValue ) person . Properties . Single ( p => p . Name == "LastName" ) . Value ;
309
+ Assert . Equal ( "John" , firstName . Value ) ;
310
+ Assert . Equal ( "Smith" , lastName . Value ) ;
311
+ }
312
+
313
+ [ Fact ]
314
+ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary ( )
315
+ {
316
+ var t = SetUp ( LogLevel . Trace ) ;
317
+ var logger = t . Item1 ;
318
+ var sink = t . Item2 ;
319
+
320
+ using ( logger . BeginScope ( new Dictionary < string , object > { { "@Person" , new Person { FirstName = "John" , LastName = "Smith" } } } ) )
321
+ {
322
+ logger . Log ( LogLevel . Information , 0 , TestMessage , null , null ) ;
323
+ }
324
+
325
+ Assert . Equal ( 1 , sink . Writes . Count ) ;
326
+ Assert . True ( sink . Writes [ 0 ] . Properties . ContainsKey ( "Person" ) ) ;
327
+
328
+ var person = ( StructureValue ) sink . Writes [ 0 ] . Properties [ "Person" ] ;
329
+ var firstName = ( ScalarValue ) person . Properties . Single ( p => p . Name == "FirstName" ) . Value ;
330
+ var lastName = ( ScalarValue ) person . Properties . Single ( p => p . Name == "LastName" ) . Value ;
331
+ Assert . Equal ( "John" , firstName . Value ) ;
332
+ Assert . Equal ( "Smith" , lastName . Value ) ;
333
+ }
334
+
335
+ [ Fact ]
336
+ public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate ( )
337
+ {
338
+ var t = SetUp ( LogLevel . Trace ) ;
339
+ var logger = t . Item1 ;
340
+ var sink = t . Item2 ;
341
+
342
+ using ( logger . BeginScope ( "{FirstName}" , "John" ) )
343
+ {
344
+ logger . Log ( LogLevel . Information , 0 , TestMessage , null , null ) ;
345
+ }
346
+
347
+ Assert . Equal ( 1 , sink . Writes . Count ) ;
348
+ Assert . True ( sink . Writes [ 0 ] . Properties . ContainsKey ( "FirstName" ) ) ;
349
+ }
350
+
351
+ [ Fact ]
352
+ public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInDictionary ( )
353
+ {
354
+ var t = SetUp ( LogLevel . Trace ) ;
355
+ var logger = t . Item1 ;
356
+ var sink = t . Item2 ;
357
+
358
+ using ( logger . BeginScope ( new Dictionary < string , object > { { "FirstName" , "John" } } ) )
359
+ {
360
+ logger . Log ( LogLevel . Information , 0 , TestMessage , null , null ) ;
361
+ }
362
+
363
+ Assert . Equal ( 1 , sink . Writes . Count ) ;
364
+ Assert . True ( sink . Writes [ 0 ] . Properties . ContainsKey ( "FirstName" ) ) ;
365
+ }
366
+
291
367
private class FoodScope : IEnumerable < KeyValuePair < string , object > >
292
368
{
293
369
readonly string _name ;
@@ -327,5 +403,11 @@ IEnumerator IEnumerable.GetEnumerator()
327
403
return GetEnumerator ( ) ;
328
404
}
329
405
}
406
+
407
+ private class Person
408
+ {
409
+ public string FirstName { get ; set ; }
410
+ public string LastName { get ; set ; }
411
+ }
330
412
}
331
413
}
0 commit comments