Exemple #1
0
        /// <summary>
        /// Demonstrates the Rollbar usage.
        /// </summary>
        private static void DemonstrateRollbarUsage()
        {
            TelemetryCollector.Instance.Capture(
                new Telemetry(
                    TelemetrySource.Client,
                    TelemetryLevel.Info,
                    new LogTelemetry("Info log telemetry")
                    )
                );

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

            customFields.Add("Hebrew", "אספירין");
            customFields.Add("Hindi", "एस्पिरि");
            customFields.Add("Chinese", "阿司匹林");
            customFields.Add("Japanese", "アセチルサリチル酸");
            customFields.Add("path1", "d:\\Work\\\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\\branches\\v2\\...");
            customFields.Add("path2", @"d:\Work\אספירין\branches\v2\...");

            var exceptionWithData = new ArgumentNullException("Exception with Data");

            exceptionWithData.Data["argumentName"] = "requiredOne";

            RollbarLocator.RollbarInstance
            .Info("ConsoleApp sample: Basic info log example.", customFields);

            TelemetryCollector.Instance.Capture(
                new Telemetry(
                    TelemetrySource.Client,
                    TelemetryLevel.Info,
                    new LogTelemetry("Something interesting happened.")
                    )
                );
            RollbarLocator.RollbarInstance
            .Debug("ConsoleApp sample: First debug log.");

            TelemetryCollector.Instance.Capture(
                new Telemetry(
                    TelemetrySource.Client,
                    TelemetryLevel.Error,
                    new ErrorTelemetry(new System.Exception("Worth mentioning!"))
                    )
                );
            RollbarLocator.RollbarInstance
            .Error(new NullReferenceException("ConsoleApp sample: null reference exception."));

            TelemetryCollector.Instance.Capture(
                new Telemetry(
                    TelemetrySource.Client,
                    TelemetryLevel.Error,
                    new ManualTelemetry(new Dictionary <string, object>()
            {
                { "somthing", "happened" },
            })
                    )
                );
            RollbarLocator.RollbarInstance
            .Error(new System.Exception("ConsoleApp sample: trying out the TraceChain", new NullReferenceException()));


            TelemetryCollector.Instance.Capture(
                new Telemetry(
                    TelemetrySource.Client,
                    TelemetryLevel.Error,
                    new ManualTelemetry(new Dictionary <string, object>()
            {
                { "param1", "value1" }, { "param2", "value2" },
            })
                    )
                );
            RollbarLocator.RollbarInstance
            .Error(exceptionWithData, customFields)
            ;

            var demoObj = new InstanceType();

            demoObj.DemonstrateStateCapture();

            Stopwatch stopwatch = Stopwatch.StartNew();

            RollbarLocator.RollbarInstance
            .Info("Via no-blocking mechanism.")
            ;
            stopwatch.Stop();
            string msg = "*** 1. No-blocking report took " + stopwatch.Elapsed.TotalMilliseconds + " [msec].";

            System.Diagnostics.Trace.WriteLine(msg);
            Console.WriteLine(msg);

            stopwatch = Stopwatch.StartNew();
            try
            {
                RollbarLocator.RollbarInstance.AsBlockingLogger(TimeSpan.FromMilliseconds(10000))
                .Info("Via blocking mechanism.")
                ;
            }
            catch (System.TimeoutException ex)
            {
                msg = "*** Blocking call with too short timeout. Exception: " + Environment.NewLine + ex;
                System.Diagnostics.Trace.WriteLine(msg);
                Console.WriteLine(msg);
            }
            stopwatch.Stop();
            msg = "*** 2. Blocking (long timeout) report took " + stopwatch.Elapsed.TotalMilliseconds + " [msec].";
            System.Diagnostics.Trace.WriteLine(msg);
            Console.WriteLine(msg);

            stopwatch = Stopwatch.StartNew();
            try
            {
                RollbarLocator.RollbarInstance.AsBlockingLogger(TimeSpan.FromMilliseconds(500))
                .Info("Via blocking mechanism with short timeout.")
                ;
            }
            catch (System.TimeoutException ex)
            {
                msg = "*** 3. Blocking call with too short timeout. Exception: " + Environment.NewLine + ex;
                System.Diagnostics.Trace.WriteLine(msg);
                Console.WriteLine(msg);
            }
            stopwatch.Stop();
            msg = "*** 3. Blocking (short timeout) report took " + stopwatch.Elapsed.TotalMilliseconds + " [msec].";
            System.Diagnostics.Trace.WriteLine(msg);
            Console.WriteLine(msg);
        }