예제 #1
0
        private AirbrakeTraceLine[] BuildRecursiveBacktrace(Exception outerMostException, out MethodBase catchingMethod, out Exception innerMostException)
        {
            Stack <AirbrakeTraceLine[]> stacks = new Stack <AirbrakeTraceLine[]>();

            catchingMethod = null;
            Exception currentException = innerMostException = outerMostException;

            while (currentException != null)
            {
                innerMostException = currentException;

                AirbrakeTraceLine[] backtrace = BuildBacktrace(currentException, out catchingMethod);

                if (currentException == outerMostException || !(backtrace.Length == 1 && backtrace[0].File == "none"))
                {
                    stacks.Push(backtrace);
                }

                if (currentException.InnerException != null)
                {
                    string rethrownMessage = String.Format("Rethrown by {0}: {1}", currentException.GetType().FullName, currentException.Message);

                    AirbrakeTraceLine rethrownLine = new AirbrakeTraceLine(rethrownMessage, 0);
                    stacks.Push(new AirbrakeTraceLine[] { rethrownLine });
                }

                currentException = currentException.InnerException;
            }

            return(stacks.SelectMany(s => s).ToArray());
        }
예제 #2
0
        private TraceLineResult GetTraceLines(IEnumerable <StackFrame> stackFrames)
        {
            var lines = new List <AirbrakeTraceLine>();

            MethodBase method;
            MethodBase catchingMethod = null;

            foreach (StackFrame frame in stackFrames)
            {
                method = frame.GetMethod();

                if (catchingMethod == null)
                {
                    catchingMethod = method;
                }

                int lineNumber = frame.GetFileLineNumber();

                if (lineNumber == 0)
                {
                    _log.Debug(f => f("No line number found in {0}, using IL offset instead.", method));
                    lineNumber = frame.GetILOffset();
                }

                string file = frame.GetFileName();

                if (String.IsNullOrEmpty(file))
                {
                    // ReSharper disable ConditionIsAlwaysTrueOrFalse
                    file = method.ReflectedType != null
                               ? method.ReflectedType.FullName
                               : "(unknown)";
                    // ReSharper restore ConditionIsAlwaysTrueOrFalse
                }

                var line = new AirbrakeTraceLine(file, lineNumber)
                {
                    Method = method.Name
                };

                lines.Add(line);
            }

            if (!lines.Any())
            {
                lines.Add(new AirbrakeTraceLine("none", 0));
            }

            if (catchingMethod == null)
            {
                catchingMethod = GetCatchingMethod();
            }

            return(new TraceLineResult {
                CatchingMethod = catchingMethod, TraceLines = lines
            });
        }
        public static AirbrakeError ToAirBrakeError(this LogEventInfo logEvent)
        {
            var error = Activator.CreateInstance <AirbrakeError>();

            if (logEvent.HasStackTrace)
            {
                StackFrame[] frames = logEvent.StackTrace.GetFrames();

                if (frames == null || frames.Length == 0)
                {
                    // Airbrake requires that at least one line is present in the XML.
                    error.Backtrace = new AirbrakeTraceLine[1] {
                        new AirbrakeTraceLine("none", 0)
                    };
                }

                List <AirbrakeTraceLine> lines = new List <AirbrakeTraceLine>();
                foreach (StackFrame frame in frames)
                {
                    var method = frame.GetMethod();

                    int lineNumber = frame.GetFileLineNumber();

                    if (lineNumber == 0)
                    {
                        //this.log.Debug(f => f("No line number found in {0}, using IL offset instead.", method));
                        lineNumber = frame.GetILOffset();
                    }

                    string file = frame.GetFileName();

                    if (String.IsNullOrEmpty(file))
                    {
                        // ReSharper disable ConditionIsAlwaysTrueOrFalse
                        file = method.ReflectedType != null
                                   ? method.ReflectedType.FullName
                                   : "(unknown)";
                        // ReSharper restore ConditionIsAlwaysTrueOrFalse
                    }

                    AirbrakeTraceLine line = new AirbrakeTraceLine(file, lineNumber)
                    {
                        Method = method.Name
                    };

                    lines.Add(line);
                }

                error.Backtrace = lines.ToArray();
            }
            error.Class = logEvent.LoggerName;
            return(error);
        }
예제 #4
0
        public List <AirbrakeTraceLine> GetBackTraceLines(Exception ex, int exceptionCount)
        {
            List <AirbrakeTraceLine> lines = new List <AirbrakeTraceLine>();
            var stackTrace = new StackTrace(ex);

            StackFrame[] frames = stackTrace.GetFrames();

            foreach (StackFrame frame in frames)
            {
                MethodBase method = frame.GetMethod();

                int lineNumber = frame.GetFileLineNumber();
                lines.Add(new AirbrakeTraceLine("---- INNER EXCEPTION ----", 0));

                if (lineNumber == 0)
                {
                    lineNumber = frame.GetILOffset();
                }

                if (lineNumber == -1)
                {
                    lineNumber = frame.GetNativeOffset();
                }

                // AirBrake doesn't allow for negative line numbers which can happen with lambdas
                if (lineNumber < 0)
                {
                    lineNumber = 0;
                }

                string file = frame.GetFileName();

                if (String.IsNullOrEmpty(file))
                {
                    file = method.ReflectedType != null
                  ? method.ReflectedType.FullName
                  : "(unknown)";
                }

                AirbrakeTraceLine line = new AirbrakeTraceLine(file, lineNumber)
                {
                    Method = method.Name
                };

                lines.Add(line);
            }
            exceptionCount++;
            if (ex.InnerException != null && exceptionCount <= maxException)
            {
                lines.AddRange(GetBackTraceLines(ex.InnerException, exceptionCount));
            }
            return(lines);
        }
예제 #5
0
        public void Building_error_from_dotNET_exception()
        {
            Exception exception;

            try
            {
                throw new InvalidOperationException("test error");
            }
            catch (Exception testException)
            {
                exception = testException;
            }

            AirbrakeError error = this.builder.ErrorFromException(exception);

            Assert.AreNotEqual(0, error.Backtrace.Length);

            AirbrakeTraceLine trace = error.Backtrace[0];

            Assert.AreEqual("Building_error_from_dotNET_exception", trace.Method);
            Assert.AreNotEqual(0, trace.LineNumber);
        }
        public void Building_error_from_dotNET_exception()
        {
            Exception exception;

            try
            {
                throw new InvalidOperationException("test error");
            }
            catch (Exception testException)
            {
                exception = testException;
            }

            AirbrakeError error = this.builder.ErrorFromException(exception);

            Assert.That(error.Backtrace, Has.Length.GreaterThan(0));

            AirbrakeTraceLine trace = error.Backtrace[0];

            Assert.That(trace.Method, Is.EqualTo("Building_error_from_dotNET_exception"));
            Assert.That(trace.LineNumber, Is.GreaterThan(0));
        }
예제 #7
0
        private AirbrakeTraceLine[] BuildBacktrace(Exception exception, out MethodBase catchingMethod)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();

            if (assembly.EntryPoint == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            if (assembly.EntryPoint == null)
            {
                assembly = Assembly.GetEntryAssembly();
            }

            catchingMethod = assembly == null
                                 ? null
                                 : assembly.EntryPoint;

            List <AirbrakeTraceLine> lines = new List <AirbrakeTraceLine>();
            var stackTrace = new StackTrace(exception);

            StackFrame[] frames = stackTrace.GetFrames();

            if (frames == null || frames.Length == 0)
            {
                // Airbrake requires that at least one line is present in the XML.
                AirbrakeTraceLine line = new AirbrakeTraceLine("none", 0);
                lines.Add(line);
                return(lines.ToArray());
            }

            foreach (StackFrame frame in frames)
            {
                MethodBase method = frame.GetMethod();

                catchingMethod = method;

                int lineNumber = frame.GetFileLineNumber();

                if (lineNumber == 0)
                {
                    this.log.Debug(f => f("No line number found in {0}, using IL offset instead.", method));
                    lineNumber = frame.GetILOffset();
                }

                string file = frame.GetFileName();

                if (String.IsNullOrEmpty(file))
                {
                    // ReSharper disable ConditionIsAlwaysTrueOrFalse
                    file = method.ReflectedType != null
                               ? method.ReflectedType.FullName
                               : "(unknown)";
                    // ReSharper restore ConditionIsAlwaysTrueOrFalse
                }

                AirbrakeTraceLine line = new AirbrakeTraceLine(file, lineNumber)
                {
                    Method = method.Name
                };

                lines.Add(line);
            }

            return(lines.ToArray());
        }