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;

        }
Exemplo n.º 2
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, true);
            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();
        }
Exemplo n.º 3
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;
    }