@@ -190,8 +190,9 @@ public void PrettyPrintJson_FormatsNestedStructures()
190190 public void PrettyPrintJson_EmptyCollectionsFormatCorrectly ( )
191191 {
192192 var emptyArray = new LogEventProperty ( "EmptyArray" , new SequenceValue ( Array . Empty < LogEventPropertyValue > ( ) ) ) ;
193+ var emptyDict = new LogEventProperty ( "EmptyDict" , new DictionaryValue ( new Dictionary < ScalarValue , LogEventPropertyValue > ( ) ) ) ;
193194 var emptyObject = new LogEventProperty ( "EmptyObject" , new StructureValue ( Array . Empty < LogEventProperty > ( ) , null ) ) ;
194- var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "Array: {EmptyArray:j}, Object: {EmptyObject:j}" ) , new [ ] { emptyArray , emptyObject } ) ;
195+ var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "Array: {EmptyArray:j}, Dict: {EmptyDict:j}, Object: {EmptyObject:j}" ) , new [ ] { emptyArray , emptyDict , emptyObject } ) ;
195196
196197 var options = new RichTextBoxSinkOptions (
197198 theme : _defaultTheme ,
@@ -201,6 +202,7 @@ public void PrettyPrintJson_EmptyCollectionsFormatCorrectly()
201202
202203 var result = RenderAndGetText ( logEvent , "{Message:l}" , options ) ;
203204 Assert . Contains ( "Array: []" , result ) ;
205+ Assert . Contains ( "Dict: {}" , result ) ;
204206 Assert . Contains ( "Object: {}" , result ) ;
205207 }
206208
@@ -249,9 +251,75 @@ public void CompactJson_StillWorksByDefault()
249251 {
250252 var complexProp = new LogEventProperty ( "ComplexProp" , new StructureValue ( new [ ] { new LogEventProperty ( "Id" , new ScalarValue ( 123 ) ) } , "MyObj" ) ) ;
251253 var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "Value: {ComplexProp}" ) , new [ ] { complexProp } ) ;
252-
253- // Default behavior (compact)
254254 Assert . Equal ( "Value: {\" Id\" : 123, \" $type\" : \" MyObj\" }" , RenderAndGetText ( logEvent , "{Message:j}" ) ) ;
255255 }
256+
257+ [ Fact ]
258+ public void ScalarValue_IFormattableButNotNumericValueType ( )
259+ {
260+ var enumValue = System . Reflection . BindingFlags . Public | System . Reflection . BindingFlags . Instance ;
261+ var prop = new LogEventProperty ( "EnumProp" , new ScalarValue ( enumValue ) ) ;
262+ var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "{EnumProp:j}" ) , new [ ] { prop } ) ;
263+
264+ var result = RenderAndGetText ( logEvent , "{Message:j}" ) ;
265+ Assert . NotNull ( result ) ;
266+ Assert . NotEmpty ( result ) ;
267+ }
268+
269+ [ Fact ]
270+ public void ScalarValue_NonFormattableObject ( )
271+ {
272+ var plainObject = new object ( ) ;
273+ var prop = new LogEventProperty ( "ObjectProp" , new ScalarValue ( plainObject ) ) ;
274+ var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "{ObjectProp:j}" ) , new [ ] { prop } ) ;
275+
276+ var result = RenderAndGetText ( logEvent , "{Message:j}" ) ;
277+ Assert . NotNull ( result ) ;
278+ Assert . NotEmpty ( result ) ;
279+ Assert . StartsWith ( "\" " , result ) ;
280+ Assert . EndsWith ( "\" " , result ) ;
281+ }
282+
283+ [ Fact ]
284+ public void PrettyPrintJson_DictionaryWithNullKey ( )
285+ {
286+ var dict = new Dictionary < ScalarValue , LogEventPropertyValue >
287+ {
288+ { new ScalarValue ( null ) , new ScalarValue ( "value" ) }
289+ } ;
290+ var dictProp = new LogEventProperty ( "DictProp" , new DictionaryValue ( dict ) ) ;
291+ var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "{DictProp:j}" ) , new [ ] { dictProp } ) ;
292+
293+ var options = new RichTextBoxSinkOptions (
294+ theme : _defaultTheme ,
295+ prettyPrintJson : true ,
296+ indentSize : 4 ,
297+ useSpacesForIndent : true ) ;
298+
299+ var result = RenderAndGetText ( logEvent , "{Message:l}" , options ) ;
300+ Assert . Contains ( "\" null\" " , result ) ;
301+ Assert . Contains ( "\" value\" " , result ) ;
302+ }
303+
304+ [ Fact ]
305+ public void PrettyPrintJson_DictionaryWithNonStringKey ( )
306+ {
307+ var dict = new Dictionary < ScalarValue , LogEventPropertyValue >
308+ {
309+ { new ScalarValue ( 123 ) , new ScalarValue ( "value" ) }
310+ } ;
311+ var dictProp = new LogEventProperty ( "DictProp" , new DictionaryValue ( dict ) ) ;
312+ var logEvent = new LogEvent ( DateTimeOffset . Now , LogEventLevel . Information , null , _parser . Parse ( "{DictProp:j}" ) , new [ ] { dictProp } ) ;
313+
314+ var options = new RichTextBoxSinkOptions (
315+ theme : _defaultTheme ,
316+ prettyPrintJson : true ,
317+ indentSize : 4 ,
318+ useSpacesForIndent : true ) ;
319+
320+ var result = RenderAndGetText ( logEvent , "{Message:l}" , options ) ;
321+ Assert . Contains ( "\" 123\" " , result ) ;
322+ Assert . Contains ( "\" value\" " , result ) ;
323+ }
256324 }
257325}
0 commit comments