C# Conversion

Can someone convert this for me?  When I convert this and try to use it, my var exception is always null and it shouldn't be as I am purposefully causing an exception to test this out.  When it encounters the while statement (perform until in .net Cobol) it will bypass the sb.appendline as exception is always null.


public static class ExceptionExtension
            public static string ToFullBlownString(this System.Exception e, int level = int.MaxValue)
                var sb = new StringBuilder();
                var exception = e;
                var counter = 1;
                while (exception != null && counter <= level)
                  sb.AppendLine($"{counter}-> Level: {counter}");
                  sb.AppendLine($"{counter}-> Message: {exception.Message}");
                  sb.AppendLine($"{counter}-> Source: {exception.Source}");
                  sb.AppendLine($"{counter}-> Target Site: {exception.TargetSite}");
                  sb.AppendLine($"{counter}-> Stack Trace: {exception.StackTrace}");

                  exception = exception.InnerException;
                  counter  ;

            return sb.ToString();
  • Verified Answer

    The following works for me:


           program-id. Program1 as "testexcept.Program1".
           procedure division.
                  call "nothere"
               catch myex as type Exception
                  display myex::ToFullBlownString
               display "after exception"
           end program Program1.
           class-id ExceptionExtension static.
           method-id ToFullBlownString extension (e as type System.Exception, level as binary-long = type Int32::MaxValue)
                                                  returning result as string.
           procedure division.
               declare sb = new type StringBuilder
               declare #exception = e
               declare counter = 1
               perform until #exception = null or counter > level
                 invoke sb::AppendLine(counter::ToString & "-> Level: " & counter::ToString)
                 invoke sb::AppendLine(counter::ToString & "-> Message: " & #exception::Message)
                 invoke sb::AppendLine(counter::ToString & "-> Source: " & #exception::Source)
                 invoke sb::AppendLine(counter::ToString & "-> Target Site: " & #exception::TargetSite)
                 invoke sb::AppendLine(counter::ToString & "-> Stack Trace: " & #exception::StackTrace)
                 set #exception = #exception::InnerException
                 set counter = counter   1
               set result to sb::ToString
           end method.
           end class.