@@ -288,6 +288,106 @@ public void WhenDisposeIsTrueProvidedLoggerIsDisposed()
288288 Assert . True ( logger . IsDisposed ) ;
289289 }
290290
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+
367+ [ Fact ]
368+ public void NamedScopesAreCaptured ( )
369+ {
370+ var t = SetUp ( LogLevel . Trace ) ;
371+ var logger = t . Item1 ;
372+ var sink = t . Item2 ;
373+
374+ using ( logger . BeginScope ( "Outer" ) )
375+ using ( logger . BeginScope ( "Inner" ) )
376+ {
377+ logger . Log ( LogLevel . Information , 0 , TestMessage , null , null ) ;
378+ }
379+
380+ Assert . Equal ( 1 , sink . Writes . Count ) ;
381+
382+ LogEventPropertyValue scopeValue ;
383+ Assert . True ( sink . Writes [ 0 ] . Properties . TryGetValue ( SerilogLoggerProvider . ScopePropertyName , out scopeValue ) ) ;
384+
385+ var items = ( scopeValue as SequenceValue ) ? . Elements . Select ( e => ( ( ScalarValue ) e ) . Value ) . Cast < string > ( ) . ToArray ( ) ;
386+ Assert . Equal ( 2 , items . Length ) ;
387+ Assert . Equal ( "Outer" , items [ 0 ] ) ;
388+ Assert . Equal ( "Inner" , items [ 1 ] ) ;
389+ }
390+
291391 private class FoodScope : IEnumerable < KeyValuePair < string , object > >
292392 {
293393 readonly string _name ;
@@ -327,5 +427,11 @@ IEnumerator IEnumerable.GetEnumerator()
327427 return GetEnumerator ( ) ;
328428 }
329429 }
430+
431+ private class Person
432+ {
433+ public string FirstName { get ; set ; }
434+ public string LastName { get ; set ; }
435+ }
330436 }
331437}
0 commit comments