Exemplo n.º 1
0
        internal static Throwable ToThrowable(Exception exception)
        {
            while (exception is AggregateException)
            {
                exception = exception.InnerException;
            }

            var throwable = exception as Throwable;

            if (throwable != null)
            {
                return(throwable);
            }

            var exceptionMessage = $"{exception.GetType().FullName}: {exception.Message}";

            throwable = new Throwable(exceptionMessage);

            var stackTrace = new List <StackTraceElement>();

            foreach (Match match in StackTraceRegex.Matches(exception.StackTrace))
            {
                var cls        = match.Groups["ClassName"].Value;
                var method     = match.Groups["MethodName"].Value;
                var methodArgs = match.Groups["MethodArguments"].Value;
                var file       = match.Groups["Filename"].Value;
                var offset     = match.Groups["Offset"].Value;

                // -2 is Java standard for native crash without line number
                int line;
                if (int.TryParse(match.Groups["LineNumber"].Value, out line))
                {
                    if (line == 0)
                    {
                        line = -2;
                    }
                }
                else
                {
                    line = -2;
                }

                offset = offset.Split('+', ' ').LastOrDefault() ?? "";

                // Filename is important to Crashlytics. Without it the crashes
                // aggregate together.
                file = $"{cls}.{method}.{offset}.cs";

                stackTrace.Add(new StackTraceElement(cls, method + methodArgs, file, line));
            }
            throwable.SetStackTrace(stackTrace.ToArray());

            if (exception.InnerException != null)
            {
                throwable.InitCause(ToThrowable(exception.InnerException));
            }

            return(throwable);
        }
Exemplo n.º 2
0
        internal static Throwable ToThrowable(Exception exception)
        {
            while (exception is AggregateException)
            {
                exception = exception.InnerException;
            }

            var throwable = exception as Throwable;

            if (throwable != null)
            {
                return(throwable);
            }

            throwable = new Throwable(exception.Message);

            var stackTrace = new List <StackTraceElement>();

            if (exception.StackTrace != null)
            {
                foreach (Match match in StackTraceRegex.Matches(exception.StackTrace))
                {
                    var cls    = match.Groups[1].Value;
                    var method = match.Groups[2].Value;
                    var file   = match.Groups[3].Value;
                    var line   = Convert.ToInt32(match.Groups[4].Value);
                    if (!cls.StartsWith("System.Runtime.ExceptionServices") &&
                        !cls.StartsWith("System.Runtime.CompilerServices"))
                    {
                        if (String.IsNullOrEmpty(file))
                        {
                            file = "filename unknown";
                        }

                        stackTrace.Add(new StackTraceElement(cls, method, file, line));
                    }
                }
            }

            throwable.SetStackTrace(stackTrace.ToArray());

            if (exception.InnerException != null)
            {
                throwable.InitCause(ToThrowable(exception.InnerException));
            }

            return(throwable);
        }