Ejemplo n.º 1
0
        public ExceptionModel LoadException(Exception exception, StackFrameModel[] existingTrace = null, Func <Exception, StackFrame[]> stackFrameGetter = null,
                                            Func <StackFrame, string> methodFormatter            = null)
        {
            stackFrameGetter = stackFrameGetter ?? (ex => new StackTrace(ex, true).GetFrames());

            var m = new ExceptionModel {
                Message           = exception.Message,
                OriginalException = exception,
                TypeName          = exception.GetType().FullName
            };

            var  frames   = stackFrameGetter(exception) ?? new StackFrame[0];
            var  stack    = new List <StackFrameModel>();
            bool skipping = existingTrace != null;

            for (int i = frames.Length - 1; i >= 0; i--)
            {
                var f = frames[i];
                if (skipping && existingTrace.Length > i && f.GetMethod() == existingTrace[i].Method)
                {
                    continue;
                }
                skipping = false;

                stack.Add(AddMoreInfo(new StackFrameModel {
                    Method          = f.GetMethod(),
                    FormattedMethod = methodFormatter?.Invoke(f),
                    At = LoadSourcePiece(f.GetFileName(), f.GetFileLineNumber(),
                                         errorColumn: f.GetFileColumnNumber())
                }));

                //Adding additional information to ExceptionModel from InfoLoaders and InfoCollectionLoader
                m.AdditionalInfo = InfoLoaders.Select(info => info(exception))
                                   .Where(info => info != null && info.Objects != null).ToArray()
                                   .Union(InfoCollectionLoader.Select(infoCollection => infoCollection(exception))
                                          .Where(infoCollection => infoCollection != null)
                                          .SelectMany(infoCollection => infoCollection)
                                          .Where(info => info != null && info.Objects != null).ToArray())
                                   .ToArray();
            }
            stack.Reverse();
            m.Stack = stack.ToArray();
            if (exception.InnerException != null)
            {
                m.InnerException = LoadException(exception.InnerException, m.Stack, stackFrameGetter, methodFormatter);
            }
            return(m);
        }
Ejemplo n.º 2
0
        public ExceptionModel LoadException(Exception exception, StackFrameModel[] existingTrace = null)
        {
            var m = new ExceptionModel();

            m.Message           = exception.Message;
            m.OriginalException = exception;
            m.TypeName          = exception.GetType().FullName;
            var  frames   = new StackTrace(exception, true).GetFrames() ?? new StackFrame[0];
            var  stack    = new List <StackFrameModel>();
            bool skipping = existingTrace != null;

            for (int i = frames.Length - 1; i >= 0; i--)
            {
                var f = frames[i];
                if (skipping && existingTrace.Length > i && f.GetMethod() == existingTrace[i].Method)
                {
                    continue;
                }
                skipping = false;

                stack.Add(AddMoreInfo(new StackFrameModel
                {
                    Method = f.GetMethod(),
                    At     = LoadSourcePiece(f.GetFileName(), f.GetFileLineNumber(),
                                             errorColumn: f.GetFileColumnNumber())
                }));

                m.AdditionalInfo = InfoLoaders.Select(info => info(exception)).Where(info => info != null && info.Objects != null).ToArray();
            }
            stack.Reverse();
            m.Stack = stack.ToArray();
            if (exception.InnerException != null)
            {
                m.InnerException = LoadException(exception.InnerException, m.Stack);
            }
            return(m);
        }