Example #1
0
        /// <summary>
        /// Composes the rolbar package.
        /// </summary>
        /// <typeparam name="TState">The type of the t state.</typeparam>
        /// <param name="eventId">The event identifier.</param>
        /// <param name="state">The state.</param>
        /// <param name="exception">The exception.</param>
        /// <param name="formatter">The formatter.</param>
        /// <returns>IRollbarPackage (if any) or null.</returns>
        protected virtual IRollbarPackage ComposeRolbarPackage <TState>(
            mslogging.EventId eventId
            , TState state
            , Exception exception
            , Func <TState, Exception, string> formatter
            )
        {
            string message = null;

            if (formatter != null)
            {
                message = formatter(state, exception);
            }

            IRollbarPackage rollbarPackage = null;

            if (exception != null)
            {
                rollbarPackage = new ExceptionPackage(exception, exception.Message);
            }
            else if (!string.IsNullOrWhiteSpace(message))
            {
                rollbarPackage = new MessagePackage(message, message);
            }
            else
            {
                return(null); //nothing to report...
            }

            Dictionary <string, object> customProperties = new Dictionary <string, object>();

            customProperties.Add(
                "LogEventID"
                , $"{eventId.Id}" + (string.IsNullOrWhiteSpace(eventId.Name) ? string.Empty : $" ({eventId.Name})")
                );
            if (exception != null && message != null)
            {
                customProperties.Add("LogMessage", message);
            }
            if (!string.IsNullOrWhiteSpace(this._name))
            {
                customProperties.Add("RollbarLoggerName", this._name);
            }
            if (customProperties.Count > 0)
            {
                rollbarPackage = new CustomKeyValuePackageDecorator(rollbarPackage, customProperties);
            }

            return(rollbarPackage);
        }
Example #2
0
        private void Page_Error(object sender, EventArgs e)
        {
            // Get last error from the server.
            Exception exception = Server.GetLastError();

            // Let's report to Rollbar on the Page Level:
            var metaData = new Dictionary <string, object>();

            metaData.Add("reportLevel", "PageLevel");
            metaData.Add("failedPage", this.AppRelativeVirtualPath);
            IRollbarPackage package = new ExceptionPackage(exception, "Page_Error", true);

            package = new CustomKeyValuePackageDecorator(package, metaData);
            package = new HttpContextPackageDecorator(package, this.Context);
            RollbarLocator.RollbarInstance.Error(package);

            // Handle specific exception.
            if (exception is InvalidOperationException)
            {
                // Pass the error on to the error page.
                Server.Transfer("ErrorPage.aspx?handler=Page_Error%20-%20Default.aspx", true);
            }
        }
Example #3
0
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <typeparam name="TState"></typeparam>
        /// <param name="logLevel">Entry will be written on this level.</param>
        /// <param name="eventId">Id of the event.</param>
        /// <param name="state">The entry to be written. Can be also an object.</param>
        /// <param name="exception">The exception related to this entry.</param>
        /// <param name="formatter">Function to create a <c>string</c> message of the <paramref name="state" /> and <paramref name="exception" />.</param>
        public void Log <TState>(
            mslogging.LogLevel logLevel
            , mslogging.EventId eventId
            , TState state
            , Exception exception
            , Func <TState, Exception, string> formatter
            )
        {
            if (!this.IsEnabled(logLevel))
            {
                return;
            }

            if (object.Equals(state, default(TState)) && exception == null)
            {
                return;
            }

            if (RollbarScope.Current != null &&
                RollbarLocator.RollbarInstance.Config.MaxItems > 0
                )
            {
                RollbarScope.Current.IncrementLogItemsCount();
                if (RollbarScope.Current.LogItemsCount == RollbarLocator.RollbarInstance.Config.MaxItems)
                {
                    // the Rollbar SDK just reached MaxItems limit, report this fact and pause further logging within this scope:
                    RollbarLocator.RollbarInstance.Warning(RollbarScope.MaxItemsReachedWarning);
                    return;
                }
                else if (RollbarScope.Current.LogItemsCount > RollbarLocator.RollbarInstance.Config.MaxItems)
                {
                    // the Rollbar SDK already exceeded MaxItems limit, do not log for this scope:
                    return;
                }
            }

            // let's custom build the Data object that includes the exception
            // along with the current HTTP request context:

            string message = null;

            if (formatter != null)
            {
                message = formatter(state, exception);
            }

            IRollbarPackage rollbarPackage = null;

            if (exception != null)
            {
                rollbarPackage = new ExceptionPackage(exception, exception.Message);
            }
            else if (!string.IsNullOrWhiteSpace(message))
            {
                rollbarPackage = new MessagePackage(message, message);
            }
            else
            {
                return; //nothing to report...
            }

            Dictionary <string, object> customProperties = new Dictionary <string, object>();

            customProperties.Add(
                "LogEventID"
                , $"{eventId.Id}" + (string.IsNullOrWhiteSpace(eventId.Name) ? string.Empty : $" ({eventId.Name})")
                );
            if (exception != null && message != null)
            {
                customProperties.Add("LogMessage", message);
            }
            if (customProperties.Count > 0)
            {
                rollbarPackage = new CustomKeyValuePackageDecorator(rollbarPackage, customProperties);
            }

            var currentContext = GetCurrentContext();

            if (currentContext != null)
            {
                rollbarPackage = new RollbarHttpContextPackageDecorator(rollbarPackage, currentContext, true);
            }

            var rollbarErrorLevel = RollbarLogger.Convert(logLevel);

            RollbarLocator.RollbarInstance.Log(rollbarErrorLevel, rollbarPackage);
        }
        public void BasicTest()
        {
            const string rollbarDataTitle = "Let's test some strategy decoration...";
            const string exceptionMessage = "Someone forgot null-test!";

            System.Exception exception = new NullReferenceException(exceptionMessage);

            IRollbarPackage packagingStrategy =
                new ExceptionPackage(exception, rollbarDataTitle);

            Assert.IsFalse(packagingStrategy.MustApplySynchronously, "Expected to be an async strategy!");

            Data rollbarData = packagingStrategy.PackageAsRollbarData();

            Assert.AreEqual(rollbarDataTitle, rollbarData.Title, "Data title is properly set!");
            Assert.IsNotNull(rollbarData.Body);
            Assert.IsNotNull(rollbarData.Body.Trace);
            Assert.IsNull(rollbarData.Body.Message);
            Assert.IsNull(rollbarData.Body.TraceChain);
            Assert.IsNull(rollbarData.Body.CrashReport);

            Assert.IsNotNull(rollbarData.Body.Trace.Exception);
            Assert.AreEqual(exceptionMessage, rollbarData.Body.Trace.Exception.Message);
            Assert.AreEqual(exception.GetType().FullName, rollbarData.Body.Trace.Exception.Class);

            Assert.IsTrue(rollbarData.Timestamp.HasValue);
            long initialTimestamp = rollbarData.Timestamp.Value;


            const string customKey1   = "customKey1";
            const string customValue1 = "customValue1";

            const string customKey2   = "customKey2";
            const string customValue2 = "customValue2";

            Dictionary <string, object> customData = new Dictionary <string, object>()
            {
                { customKey1, customValue1 },
                { customKey2, customValue2 },
            };

            packagingStrategy = new CustomKeyValuePackageDecorator(packagingStrategy, customData);

            // All the asserts prior to the decoration should be good again:

            Assert.IsFalse(packagingStrategy.MustApplySynchronously, "Expected to be an async strategy!");

            rollbarData = packagingStrategy.PackageAsRollbarData();

            Assert.AreEqual(rollbarDataTitle, rollbarData.Title, "Data title is properly set!");
            Assert.IsNotNull(rollbarData.Body);
            Assert.IsNotNull(rollbarData.Body.Trace);
            Assert.IsNull(rollbarData.Body.Message);
            Assert.IsNull(rollbarData.Body.TraceChain);
            Assert.IsNull(rollbarData.Body.CrashReport);

            Assert.IsNotNull(rollbarData.Body.Trace.Exception);
            Assert.AreEqual(exceptionMessage, rollbarData.Body.Trace.Exception.Message);
            Assert.AreEqual(exception.GetType().FullName, rollbarData.Body.Trace.Exception.Class);

            // Custom data decoration specific asserts:

            Assert.IsNotNull(rollbarData.Custom);
            Assert.AreEqual(customData.Count, rollbarData.Custom.Count);
            Assert.IsTrue(rollbarData.Custom.ContainsKey(customKey1));
            Assert.IsTrue(rollbarData.Custom.ContainsKey(customKey2));
            Assert.AreEqual(customValue1, rollbarData.Custom[customKey1]);
            Assert.AreEqual(customValue2, rollbarData.Custom[customKey2]);

            // Make sure the Data.Timestamp was not overwritten:

            Assert.IsTrue(rollbarData.Timestamp.HasValue);
            Assert.AreEqual(initialTimestamp, rollbarData.Timestamp.Value);
            System.Diagnostics.Debug.WriteLine($"Initial timestamp: {initialTimestamp}");
            System.Diagnostics.Debug.WriteLine($"Decorated timestamp: {rollbarData.Timestamp.Value}");
        }