@@ -783,6 +783,7 @@ public void AggregateExceptionWithExceptionDataSingleTest()
783783 Assert . Contains ( string . Format ( ExceptionDataFormat , exceptionDataKey , exceptionDataValue ) , lastMessage ) ;
784784 Assert . Contains ( string . Format ( ExceptionDataFormat , aggregateExceptionDataKey , aggregateExceptionDataValue ) , lastMessage ) ;
785785 }
786+
786787 [ Fact ]
787788 public void CustomExceptionProperties_Layout_Test ( )
788789 {
@@ -819,6 +820,40 @@ public void BaseExceptionTest()
819820 AssertDebugLastMessage ( "debug1" , "Goodbye World" ) ;
820821 }
821822
823+ #if NET3_5
824+ [ Fact ( Skip = "NET3_5 not supporting AggregateException" ) ]
825+ #else
826+ [ Fact ]
827+ #endif
828+ public void RecursiveAsyncExceptionWithoutFlattenException ( )
829+ {
830+ var recursionCount = 3 ;
831+ Func < int > innerAction = ( ) => throw new ApplicationException ( "Life is hard" ) ;
832+ var t1 = System . Threading . Tasks . Task < int > . Factory . StartNew ( ( ) =>
833+ {
834+ return NestedFunc ( recursionCount , innerAction ) ;
835+ } ) ;
836+
837+ try
838+ {
839+ t1 . Wait ( ) ;
840+ }
841+ catch ( AggregateException ex )
842+ {
843+ var layoutRenderer = new ExceptionLayoutRenderer ( ) { Format = "ToString" , FlattenException = false } ;
844+ var logEvent = LogEventInfo . Create ( LogLevel . Error , null , null , ( object ) ex ) ;
845+ var result = layoutRenderer . Render ( logEvent ) ;
846+ int needleCount = 0 ;
847+ int foundIndex = result . IndexOf ( nameof ( NestedFunc ) , 0 ) ;
848+ while ( foundIndex >= 0 )
849+ {
850+ ++ needleCount ;
851+ foundIndex = result . IndexOf ( nameof ( NestedFunc ) , foundIndex + nameof ( NestedFunc ) . Length ) ;
852+ }
853+ Assert . True ( needleCount >= recursionCount , $ "{ needleCount } too small") ;
854+ }
855+ }
856+
822857 private class ExceptionWithBrokenMessagePropertyException : NLogConfigurationException
823858 {
824859 public override string Message => throw new Exception ( "Exception from Message property" ) ;
@@ -885,6 +920,21 @@ private Exception GetNestedExceptionWithStackTrace(string exceptionMessage)
885920 }
886921 }
887922
923+ private int NestedFunc ( int recursion , Func < int > innerAction )
924+ {
925+ try
926+ {
927+ if ( recursion -- == 0 )
928+ return System . Threading . Tasks . Task < int > . Factory . StartNew ( ( ) => innerAction . Invoke ( ) )
929+ . Result ;
930+ return NestedFunc ( recursion , innerAction ) ;
931+ }
932+ catch
933+ {
934+ throw ; // Just to make the method complex, and avoid inline
935+ }
936+ }
937+
888938 private Exception GetExceptionWithoutStackTrace ( string exceptionMessage )
889939 {
890940 return new CustomArgumentException ( exceptionMessage , "exceptionMessage" ) ;
0 commit comments