/// <summary>
        /// Creates a <see cref="AirbrakeNotice"/> from the the specified error.
        /// </summary>
        /// <param name="error">The error.</param>
        /// <returns></returns>
        public AirbrakeNotice Notice(AirbrakeError error)
        {
            this.log.Debug(f => f("{0}.Notice({1})", GetType(), error));

            var notice = new AirbrakeNotice
            {
                ApiKey            = Configuration.ApiKey,
                Error             = error,
                Notifier          = Notifier,
                ServerEnvironment = ServerEnvironment,
            };

            if (HttpContext.Current != null)
            {
                Assembly assembly = Assembly.GetExecutingAssembly();

                notice.Request = new AirbrakeRequest(HttpContext.Current.Request.Url, assembly.CodeBase)
                {
                    Params  = BuildParams().ToArray(),
                    Session = BuildSession().ToArray(),
                    CgiData = BuildCgiData().ToArray(),
                };
            }

            return(notice);
        }
        public void Notice_contains_Request()
        {
            AirbrakeNotice notice  = null;
            const string   url     = "http://example.com/?Query.Key1=Query.Value1&Query.Key2=Query.Value2";
            const string   referer = "http://github.com/";
            string         physicalApplicationPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar;
            var            httpSimulator           = new HttpSimulator("/", physicalApplicationPath)
                                                     .SetFormVariable("Form.Key1", "Form.Value1")
                                                     .SetFormVariable("Form.Key2", "Form.Value2")
                                                     .SetHeader("Header.Key1", "Header.Value1")
                                                     .SetHeader("Header.Key2", "Header.Value2")
                                                     .SetReferer(new Uri(referer))
                                                     .SimulateRequest(new Uri(url));

            using (httpSimulator)
            {
                try
                {
                    Thrower.Throw(new Exception("Halp!"));
                }
                catch (Exception exception)
                {
                    AirbrakeError error = this.builder.ErrorFromException(exception);
                    notice = this.builder.Notice(error);
                }
            }

            Console.WriteLine(CleanXmlSerializer.ToXml(notice));

            Assert.That(notice, Is.Not.Null);
            Assert.That(notice.Error, Is.Not.Null);

#if !NET35
            // We have defined a NET35 constant in the Visual Studio 2008 project so the below code isn't executed,
            // since it requires HttpSimulator which in turn requires .NET 4.0, which in turn requires Visual Studio 2010.
            Assert.That(notice.Request, Is.Not.Null);
            Assert.That(notice.Request.Url, Is.EqualTo(url));
            Assert.That(notice.Request.Component, Is.EqualTo(typeof(Thrower).FullName));
            Assert.That(notice.Request.Action, Is.EqualTo("Throw"));

            Assert.That(notice.Request.CgiData,
                        Contains.Item(new AirbrakeVar("Content-Type", "application/x-www-form-urlencoded")));
            Assert.That(notice.Request.CgiData,
                        Contains.Item(new AirbrakeVar("Header.Key1", "Header.Value1")));
            Assert.That(notice.Request.CgiData,
                        Contains.Item(new AirbrakeVar("Header.Key2", "Header.Value2")));
            Assert.That(notice.Request.CgiData, Contains.Item(new AirbrakeVar("Referer", referer)));

            Assert.That(notice.Request.Params,
                        Contains.Item(new AirbrakeVar("APPL_PHYSICAL_PATH", physicalApplicationPath)));
            Assert.That(notice.Request.Params,
                        Contains.Item(new AirbrakeVar("QUERY_STRING", "Query.Key1=Query.Value1&Query.Key2=Query.Value2")));
            Assert.That(notice.Request.Params, Contains.Item(new AirbrakeVar("Form.Key1", "Form.Value1")));
            Assert.That(notice.Request.Params, Contains.Item(new AirbrakeVar("Form.Key2", "Form.Value2")));
            Assert.That(notice.Request.Params, Contains.Item(new AirbrakeVar("Query.Key1", "Query.Value1")));
            Assert.That(notice.Request.Params, Contains.Item(new AirbrakeVar("Query.Key2", "Query.Value2")));
#endif
        }
Beispiel #3
0
        private static AirbrakeError CreateError(Exception exception)
        {
            var error = new AirbrakeError
            {
                Type      = exception.GetType().FullName,
                Message   = exception.GetType().Name + ": " + exception.Message,
                Backtrace = CreateBacktrace(exception)
            };

            return(error);
        }
        /// <summary>
        /// Creates a <see cref="AirbrakeNotice"/> from the the specified exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <returns>
        /// A <see cref="AirbrakeNotice"/>, created from the the specified exception.
        /// </returns>
        public AirbrakeNotice Notice(Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            this.log.Info(f => f("{0}.Notice({1})", GetType(), exception.GetType()), exception);

            AirbrakeError error = ErrorFromException(exception);

            return(Notice(error));
        }
        /// <summary>
        /// Creates a <see cref="AirbrakeNotice"/> from the the specified exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="extraParams">extra data for adding the params in airbrake notice </param>
        /// <returns>
        /// A <see cref="AirbrakeNotice"/>, created from the the specified exception.
        /// </returns>
        public AirbrakeNotice Notice(Exception exception, IEnumerable <KeyValuePair <string, string> > extraParams = null)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            this.log.Info(f => f("{0}.Notice({1})", GetType(), exception.GetType()), exception);

            AirbrakeError error = ErrorFromException(exception);

            return(Notice(error, extraParams));
        }
        /// <summary>
        /// Creates a <see cref="AirbrakeError"/> from the the specified exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <returns>
        /// A <see cref="AirbrakeError"/>, created from the the specified exception.
        /// </returns>
        public AirbrakeError ErrorFromException(Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            this.log.Debug(f => f("{0}.Notice({1})", GetType(), exception.GetType()), exception);

            var error = new AirbrakeError
            {
                Class     = exception.GetType().FullName,
                Message   = exception.GetType().Name + ": " + exception.Message,
                Backtrace = BuildBacktrace(exception).ToArray(),
            };

            return(error);
        }
        public void StackTrace_contains_lambda_expression()
        {
            Exception exception = null;

            try
            {
                Expression <Func <int> > inner = () => ((string)null).Length;

                inner.Compile()();
            }
            catch (Exception testException)
            {
                exception = testException;
            }

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

            Assert.That(error, Is.Not.Null);
        }
        /// <summary>
        /// Creates a <see cref="AirbrakeNotice"/> from the the specified error.
        /// </summary>
        /// <param name="error">The error.</param>
        /// <returns></returns>
        public AirbrakeNotice Notice(AirbrakeError error)
        {
            this.log.Debug(f => f("{0}.Notice({1})", GetType(), error));

            var notice = new AirbrakeNotice
            {
                ApiKey            = Configuration.ApiKey,
                Error             = error,
                Notifier          = Notifier,
                ServerEnvironment = ServerEnvironment,
            };

            MethodBase catchingMethod = (error != null)
                                            ? error.CatchingMethod
                                            : null;

            AddContextualInformation(notice, catchingMethod);

            return(notice);
        }
        public void LogEvent_WithExecption_CallsClient()
        {
            var       client = A.Fake <ISharpbrakeClient>();
            Exception ex     = new ApplicationException("something bad happened");

            // using activator to avoid the obsolete tag on the constructor.
            AirbrakeError  error  = Activator.CreateInstance <AirbrakeError>();
            AirbrakeNotice notice = new AirbrakeNotice()
            {
                Error = error
            };

            A.CallTo(() => client.BuildNotice(ex)).Returns(notice);

            AirBrakeTarget target = new AirBrakeTarget(client);

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target);

            logger.InfoException("kaboom", ex);
            A.CallTo(() => client.Send(notice)).MustHaveHappened();
        }
Beispiel #10
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));
        }
Beispiel #12
0
        /// <summary>
        /// Creates a <see cref="AirbrakeNotice"/> from the the specified exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <returns>
        /// A <see cref="AirbrakeNotice"/>, created from the the specified exception.
        /// </returns>
        public AirbrakeNotice Notice(string message, StackTrace stackTrace)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (stackTrace == null)
            {
                throw new ArgumentNullException("stackTrace");
            }

            this.log.Info(f => f("{0}.Notice({1})", GetType(), message));

            AirbrakeError error = Activator.CreateInstance <AirbrakeError>();

            MethodBase catchingMethod;

            error.Backtrace = BuildBacktrace(stackTrace, out catchingMethod);
            error.Class     = catchingMethod.DeclaringType.FullName;
            error.Message   = message;

            return(Notice(error));
        }
Beispiel #13
0
 public AirbrakeNotice BuildNotice(AirbrakeError error)
 {
     return(this.Builder.Notice(error));
 }