wasSharpNET – Rev 16

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
// Based on Oguzhan KIRCALI & CDspace @ https://stackoverflow.com/questions/4272579/how-to-print-full-stack-trace-in-exception

using System;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace wasSharpNET.Diagnostics
{
    public static class ExceptionExtensions
    {
        public static string PrettyPrint(this Exception x)
        {
            var st = new StackTrace(x, true);
            var frames = st.GetFrames();

            StringBuilder sb = new StringBuilder();
            sb.Append(Enumerable.Repeat("-", 75));

            int indent = 0;
            foreach (var frame in frames)
            {
                if (frame.GetFileLineNumber() < 1)
                    continue;

                sb.Append(Enumerable.Repeat(" ", indent));
                sb.Append(@" -> ");
                sb.Append("File: ");
                sb.Append(frame.GetFileName());
                sb.Append(@" Method: ");
                sb.Append(frame.GetMethod().Name);
                sb.Append(@" Line and Column : ");
                sb.Append(frame.GetFileLineNumber());
                sb.Append(@":");
                sb.Append(frame.GetFileColumnNumber());
                sb.Append(@"\n");

                indent += 4;
            }

            sb.Append(Enumerable.Repeat("-", 75));
            sb.Append(x);
            sb.Append(Enumerable.Repeat("-", 75));

            return sb.ToString();
        }
    }
}