コード例 #1
0
        public void TestReconfiguration()
        {
            RollbarLoggerConfig config = new RollbarLoggerConfig();

            Assert.AreEqual("seedToken", config.RollbarDestinationOptions.AccessToken);
            Console.WriteLine(config.TraceAsString());
            config.Reconfigured += Config_Reconfigured;
            Assert.AreEqual(0, this._actualReconfigCount);
            IRollbarDestinationOptions rollbarDestinationOptions = config.RollbarDestinationOptions;

            RollbarDestinationOptions destinationOptions = new RollbarDestinationOptions();

            destinationOptions.AccessToken = "CUSTOM";
            config.RollbarDestinationOptions.Reconfigure(destinationOptions);
            Assert.AreEqual(1, this._actualReconfigCount);
            Assert.AreSame(rollbarDestinationOptions, config.RollbarDestinationOptions);
            Assert.AreEqual("CUSTOM", config.RollbarDestinationOptions.AccessToken, "Options reconfig works!");
            Console.WriteLine(config.TraceAsString());

            RollbarLoggerConfig newConfig = new RollbarLoggerConfig();

            Assert.AreEqual("seedToken", newConfig.RollbarDestinationOptions.AccessToken);
            Console.WriteLine(newConfig.TraceAsString());
            Assert.AreNotSame(rollbarDestinationOptions, newConfig.RollbarDestinationOptions);
            newConfig.Reconfigured   += Config_Reconfigured;
            rollbarDestinationOptions = newConfig.RollbarDestinationOptions;

            newConfig.Reconfigure(config);
            Assert.AreEqual(8, this._actualReconfigCount);
            Assert.AreEqual("CUSTOM", newConfig.RollbarDestinationOptions.AccessToken, "Structured config's reconfig works!");
            Console.WriteLine(newConfig.TraceAsString());
            Assert.AreSame(rollbarDestinationOptions, newConfig.RollbarDestinationOptions);
            Assert.AreNotSame(config, newConfig);
            Assert.AreNotSame(config.RollbarDestinationOptions, newConfig.RollbarDestinationOptions);
        }
コード例 #2
0
        /// <summary>
        /// Enqueues the data.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <param name="level">The level.</param>
        /// <param name="custom">The custom.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="signal">The signal.</param>
        /// <returns>PayloadBundle.</returns>
        internal PayloadBundle?EnqueueData(
            object dataObject,
            ErrorLevel level,
            IDictionary <string, object?>?custom,
            TimeSpan?timeout     = null,
            SemaphoreSlim?signal = null
            )
        {
            // here is the last chance to decide if we need to actually send this payload
            // based on the current config settings and rate-limit conditions:
            if (string.IsNullOrWhiteSpace(this._config.RollbarDestinationOptions.AccessToken) ||
                !this._config.RollbarDeveloperOptions.Enabled ||
                (level < this._config.RollbarDeveloperOptions.LogLevel) ||
                ((this._payloadQueue.AccessTokenQueuesMetadata != null) && this._payloadQueue.AccessTokenQueuesMetadata.IsTransmissionSuspended)
                )
            {
                // nice shortcut:
                return(null);
            }

            if (this._config.RollbarDeveloperOptions.RethrowExceptionsAfterReporting)
            {
                System.Exception?exception = dataObject as System.Exception;
                if (exception == null &&
                    dataObject is Data data &&
                    data.Body != null
                    )
                {
                    exception = data.Body.OriginalException;
                }

                if (exception != null)
                {
                    try
                    {
                        // Here we need to create another logger instance with similar config but configured not to re-throw.
                        // This would prevent infinite recursive calls (in case if we used this instance or any re-throwing instance).
                        // Because we will be re-throwing the exception after reporting, let's report it fully-synchronously.
                        // This logic is on a heavy side. But, fortunately, RethrowExceptionsAfterReporting is intended to be
                        // a development time option:
                        var config = new RollbarLoggerConfig();
                        config.Reconfigure(this._config);
                        config.RollbarDeveloperOptions.RethrowExceptionsAfterReporting = false;
                        using var rollbar = RollbarFactory.CreateNew(config);
                        rollbar.AsBlockingLogger(TimeSpan.FromSeconds(1)).Log(level, dataObject, custom);
                    }
                    catch
                    {
                        // In case there was a TimeoutException (or any un-expected exception),
                        // there is nothing we can do here.
                        // We tried our best...
                    }
                    finally
                    {
                        if (exception is AggregateException aggregateException)
                        {
                            exception = aggregateException.Flatten();
                        }
                        ExceptionDispatchInfo.Capture(exception).Throw();
                    }

                    return(null);
                }
            }


            PayloadBundle?payloadBundle = null;

            try
            {
                payloadBundle = CreatePayloadBundle(dataObject, level, custom, timeout, signal);
            }
            catch (System.Exception exception)
            {
                RollbarErrorUtility.Report(
                    this,
                    dataObject,
                    InternalRollbarError.BundlingError,
                    null,
                    exception,
                    payloadBundle
                    );
                return(null);
            }

            try
            {
                this._payloadQueue.Enqueue(payloadBundle);
            }
            catch (System.Exception exception)
            {
                RollbarErrorUtility.Report(
                    this,
                    dataObject,
                    InternalRollbarError.EnqueuingError,
                    null,
                    exception,
                    payloadBundle
                    );
            }

            return(payloadBundle);
        }
コード例 #3
0
        public void TestAppenderReconfiguration()
        {
            Person[] expectedPersons = new Person[]
            {
                null,
                new Person("Person1"),
                new Person("Person2"),
            };

            RollbarAppender appender = new RollbarAppender(
                RollbarUnitTestSettings.AccessToken,
                RollbarUnitTestSettings.Environment,
                TimeSpan.FromSeconds(3)
                );

            string repositoryName = typeof(RollbarAppenderFixture).Name;
            var    repository     = LoggerManager.CreateRepository(repositoryName);
            string loggerName     = typeof(RollbarAppenderFixture).Name;

            BasicConfigurator.Configure(repository, appender);
            ILog log = LogManager.GetLogger(repositoryName, loggerName);


            log.Info("Via log4net");

            RollbarLoggerConfig newConfig = new RollbarLoggerConfig();

            newConfig.Reconfigure(appender.RollbarConfig);
            newConfig.RollbarPayloadAdditionOptions.Person = expectedPersons[1];
            appender.RollbarConfig.Reconfigure(newConfig);
            log.Info("Via log4net");

            newConfig = new RollbarLoggerConfig();
            newConfig.Reconfigure(appender.RollbarConfig);
            newConfig.RollbarPayloadAdditionOptions.Person = expectedPersons[2];
            RollbarDataSecurityOptions dataSecurityOptions = new RollbarDataSecurityOptions();

            dataSecurityOptions.ScrubFields = new string[]
            {
                "log4net:UserName",
                "log4net:HostName",
                "log4net:Identity",
            };
            newConfig.RollbarDataSecurityOptions.Reconfigure(dataSecurityOptions);

            appender.RollbarConfig.Reconfigure(newConfig);
            log.Info("Via log4net");

            // wait until all the payloads are processed and transmitted
            Thread.Sleep(TimeSpan.FromSeconds(5));

            Assert.IsTrue(this._rollbarCommEvents.Count == 3 || this._rollbarCommErrorEvents.Count == 3, "Either comm successes or errors are captured...");

            if (this._rollbarCommErrorEvents.Count == 3)
            {
                //this scenario happens on the CI server (Azure Pipelines):
                foreach (var commErrorEvent in this._rollbarCommErrorEvents)
                {
                    Assert.IsTrue(
                        commErrorEvent.Error.Message.Contains(
                            "Preliminary ConnectivityMonitor detected offline status!"),
                        "Matching error message."
                        );
                }
                Assert.IsFalse(this._rollbarCommErrorEvents[0].Payload.Contains("\"person\":{\"id\":"), "checking this._rollbarCommErrorEvents[0].Payload");
                Assert.IsTrue(this._rollbarCommErrorEvents[1].Payload.Contains(expectedPersons[1].Id), "checking this._rollbarCommErrorEvents[1].Payload");
                Assert.IsTrue(this._rollbarCommErrorEvents[2].Payload.Contains(expectedPersons[2].Id), "checking this._rollbarCommErrorEvents[2].Payload");
            }
            else
            {
                Assert.IsFalse(this._rollbarCommEvents[0].Payload.Contains("\"person\":{\"id\":"), "checking this._rollbarCommEvents[0].Payload");
                Assert.IsTrue(this._rollbarCommEvents[1].Payload.Contains(expectedPersons[1].Id), "checking this._rollbarCommEvents[1].Payload");
                Assert.IsTrue(this._rollbarCommEvents[2].Payload.Contains(expectedPersons[2].Id), "checking this._rollbarCommEvents[2].Payload");
            }
        }