Esempio n. 1
0
        /// <summary>
        /// Adds breadcrumb entry to the breadcrumbs file.
        /// </summary>
        /// <param name="message">Breadcrumb message</param>
        /// <param name="level">Breadcrumb level</param>
        /// <param name="type">Breadcrumb type</param>
        /// <param name="attributes">Breadcrumb attributs</param>
        /// <returns>True if breadcrumb was stored in the breadcrumbs file. Otherwise false.</returns>
        public bool Add(string message, BreadcrumbLevel level, UnityEngineLogLevel type, IDictionary <string, string> attributes)
        {
            byte[] bytes;
            lock (_lockObject)
            {
                double id         = _breadcrumbId++;
                var    jsonObject = CreateBreadcrumbJson(id, message, level, type, attributes);
                bytes = System.Text.Encoding.UTF8.GetBytes(jsonObject.ToJson());

                if (currentSize + bytes.Length > BreadcrumbsSize)
                {
                    try
                    {
                        ClearOldLogs();
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                }
            }

            try
            {
                return(AppendBreadcrumb(bytes));
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Cannot append data to the breadcrumbs file. Reason: {0}", e.Message));
                return(false);
            }
        }
Esempio n. 2
0
        private void TestAddBreadcrumbExtension(
            Action <
                string,
                string,
                string,
                IDictionary <string, string>,
                BreadcrumbLevel> action)
        {
            const string message  = "message";
            const string type     = "type";
            const string category = "category";
            var          data     = new Dictionary <string, string>
            {
                { "Key", "value" },
                { "Key2", "value2" },
            };
            const BreadcrumbLevel level = BreadcrumbLevel.Critical;

            var scope = new Scope();

            Hub.When(h => h.ConfigureScope(Arg.Any <Action <Scope> >()))
            .Do(c => c.Arg <Action <Scope> >()(scope));

            action(message, category, type, data, level);

            var crumb = scope.Breadcrumbs.First();

            Assert.Equal(message, crumb.Message);
            Assert.Equal(type, crumb.Type);
            Assert.Equal(category, crumb.Category);
            Assert.Equal(level, crumb.Level);
            Assert.Equal(data.Count, crumb.Data.Count);
            Assert.Equal(data.ToImmutableDictionary(), crumb.Data);
        }
Esempio n. 3
0
 public void AddBreadcrumb(
     string message,
     string type,
     string category = null,
     IDictionary <string, string> data = null,
     BreadcrumbLevel level             = default)
 => SentryCore.AddBreadcrumb(message, type, category, data, level);
Esempio n. 4
0
        public bool Add(string message, BreadcrumbLevel type, UnityEngineLogLevel level, IDictionary <string, string> attributes)
        {
            lock (_lockObject)
            {
                if (Breadcrumbs.Count + 1 > MaximumNumberOfBreadcrumbs)
                {
                    while (Breadcrumbs.Count + 1 > MaximumNumberOfBreadcrumbs)
                    {
                        Breadcrumbs.Dequeue();
                    }
                }
            }

            Breadcrumbs.Enqueue(new InMemoryBreadcrumb()
            {
                Message    = message,
                Timestamp  = DateTimeHelper.TimestampMs(),
                Level      = level,
                Type       = type,
                Attributes = attributes
            });
            _breadcrumbId++;

            return(true);
        }
        public void AddBreadcrumb_ValueTuple_AllArgumentsMatch()
        {
            const string          expectedMessage  = "original Message";
            const string          expectedCategory = "original Category";
            const string          expectedType     = "original Type";
            var                   expectedData     = (key : "key", value : "value");
            const BreadcrumbLevel expectedLevel    = BreadcrumbLevel.Critical;

            var sut = _fixture.GetSut();

            sut.AddBreadcrumb(
                expectedMessage,
                expectedCategory,
                expectedType,
                expectedData,
                expectedLevel);

            var actual = Assert.Single(sut.InternalBreadcrumbs);

            Assert.Equal(expectedMessage, actual.Message);
            Assert.Equal(expectedCategory, actual.Category);
            Assert.Equal(expectedType, actual.Type);
            Assert.Equal(expectedData.key, actual.Data.Single().Key);
            Assert.Equal(expectedData.value, actual.Data.Single().Value);
            Assert.Equal(expectedLevel, actual.Level);
        }
        /// <summary>
        /// Adds a breadcrumb to the scope
        /// </summary>
        /// <param name="scope">The scope.</param>
        /// <param name="message">The message.</param>
        /// <param name="type">The type.</param>
        /// <param name="category">The category.</param>
        /// <param name="dataPair">The data key-value pair.</param>
        /// <param name="level">The level.</param>
        public static void AddBreadcrumb(
            this Scope scope,
            string message,
            string category,
            string type,
            Tuple <string, string> dataPair = null,
            BreadcrumbLevel level           = 0)
        {
            Dictionary <string, string> data = null;

            if (dataPair != null)
            {
                data = new Dictionary <string, string>
                {
                    { dataPair.Item1, dataPair.Item2 }
                };
            }

            scope.AddBreadcrumb(
                timestamp: null,
                message: message,
                category: category,
                type: type,
                data: data,
                level: level);
        }
Esempio n. 7
0
        public void AddBreadcrumb_ImmutableDictionary_AllArgumentsMatch()
        {
            var expectedTimestamp = DateTimeOffset.MaxValue;
            var clock             = Substitute.For <ISystemClock>();

            clock.GetUtcNow().Returns(expectedTimestamp);
            const string expectedMessage  = "original Message";
            const string expectedCategory = "original Category";
            const string expectedType     = "original Type";
            var          expectedData     = ImmutableDictionary <string, string> .Empty.Add("key", "val");

            const BreadcrumbLevel expectedLevel = BreadcrumbLevel.Critical;

            _sut.AddBreadcrumb(
                clock,
                expectedMessage,
                expectedCategory,
                expectedType,
                expectedData,
                expectedLevel);

            var actual = Assert.Single(_sut.InternalBreadcrumbs);

            Assert.Equal(expectedTimestamp, actual.Timestamp);
            Assert.Equal(expectedMessage, actual.Message);
            Assert.Equal(expectedCategory, actual.Category);
            Assert.Equal(expectedType, actual.Type);
            Assert.Equal(expectedData.Single().Key, actual.Data.Single().Key);
            Assert.Equal(expectedData.Single().Value, actual.Data.Single().Value);
            Assert.Equal(expectedLevel, actual.Level);
        }
Esempio n. 8
0
        public void AddBreadcrumb_Dictionary_AllArgumentsMatch()
        {
            const string expectedMessage  = "original Message";
            const string expectedCategory = "original Category";
            const string expectedType     = "original Type";
            var          expectedData     = new Dictionary <string, string>()
            {
                { "key", "val" }
            };
            const BreadcrumbLevel expectedLevel = BreadcrumbLevel.Critical;

            _sut.AddBreadcrumb(
                expectedMessage,
                expectedCategory,
                expectedType,
                expectedData,
                expectedLevel);

            var actual = Assert.Single(_sut.InternalBreadcrumbs);

            Assert.Equal(expectedMessage, actual.Message);
            Assert.Equal(expectedCategory, actual.Category);
            Assert.Equal(expectedType, actual.Type);
            Assert.Equal(expectedData.Single().Key, actual.Data.Single().Key);
            Assert.Equal(expectedData.Single().Value, actual.Data.Single().Value);
            Assert.Equal(expectedLevel, actual.Level);
        }
        public void AddBreadcrumb_ImmutableDictionary_AllArgumentsMatch()
        {
            var          expectedTimestamp = DateTimeOffset.MaxValue;
            const string expectedMessage   = "original Message";
            const string expectedCategory  = "original Category";
            const string expectedType      = "original Type";
            var          expectedData      = new Dictionary <string, string>()
            {
                { "key", "val" }
            };
            const BreadcrumbLevel expectedLevel = BreadcrumbLevel.Critical;

            var sut = _fixture.GetSut();

            sut.AddBreadcrumb(
                expectedTimestamp,
                expectedMessage,
                expectedCategory,
                expectedType,
                expectedData,
                expectedLevel);

            var actual = Assert.Single(sut.InternalBreadcrumbs);

            Assert.Equal(expectedTimestamp, actual.Timestamp);
            Assert.Equal(expectedMessage, actual.Message);
            Assert.Equal(expectedCategory, actual.Category);
            Assert.Equal(expectedType, actual.Type);
            Assert.Equal(expectedData.Single().Key, actual.Data.Single().Key);
            Assert.Equal(expectedData.Single().Value, actual.Data.Single().Value);
            Assert.Equal(expectedLevel, actual.Level);
        }
Esempio n. 10
0
        public static void AddBreadcrumb(
            this IHub hub,
            ISystemClock?clock,
            string message,
            string?category = null,
            string?type     = null,
            IDictionary <string, string>?data = null,
            BreadcrumbLevel level             = default)
        {
            // Not to throw on code that ignores nullability warnings.
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (hub is null)
            {
                return;
            }

            hub.ConfigureScope(
                s => s.AddBreadcrumb(
                    (clock ?? SystemClock.Clock).GetUtcNow(),
                    message,
                    category,
                    type,
                    data != null ? new Dictionary <string, string>(data) : null,
                    level));
        }
Esempio n. 11
0
        public void Log_WithException_BreadcrumbFromException()
        {
            var expectedException = new Exception("expected message");
            const BreadcrumbLevel expectedLevel = BreadcrumbLevel.Critical;

            var sut = _fixture.GetSut();

            sut.Log <object>(LogLevel.Critical, default, null, expectedException, null);
 internal bool AddBreadcrumbs(string message, BreadcrumbLevel level, UnityEngineLogLevel type, IDictionary <string, string> attributes = null)
 {
     if (!BreadcrumbsLevel.HasFlag((BacktraceBreadcrumbType)level))
     {
         return(false);
     }
     return(LogManager.Add(message, level, type, attributes));
 }
 /// <summary>
 /// Adds a breadcrumb to the scope.
 /// </summary>
 /// <param name="scope">The scope.</param>
 /// <param name="message">The message.</param>
 /// <param name="category">The category.</param>
 /// <param name="type">The type.</param>
 /// <param name="data">The data.</param>
 /// <param name="level">The level.</param>
 public static void AddBreadcrumb(
     this Scope scope,
     string message,
     string category = null,
     string type     = null,
     Dictionary <string, string> data = null,
     BreadcrumbLevel level            = 0)
 {
     scope.AddBreadcrumb(null, message, category, type, null, level);
 }
Esempio n. 14
0
        private void Log(string message, LogType level, BreadcrumbLevel breadcrumbLevel, IDictionary <string, string> attributes = null)
        {
            var type = _breadcrumbs.ConvertLogTypeToLogLevel(level);

            if (!_breadcrumbs.ShouldLog(type))
            {
                return;
            }
            _breadcrumbs.AddBreadcrumbs(message, breadcrumbLevel, type, attributes);
        }
Esempio n. 15
0
 public static void LogSentryBreadcrumb(string category, string message, BreadcrumbLevel level = BreadcrumbLevel.Info)
 {
     Console.Out.WriteLine(String.Format("{0} : {1}", category, message));
     if (ravenClient != null)
     {
         ravenClient.AddTrail(new Breadcrumb(category)
         {
             Message = message, Type = BreadcrumbType.Navigation
         });
     }
 }
Esempio n. 16
0
 /// <summary>
 /// To Java SentryLevel.
 /// </summary>
 /// <param name="level">The Breadcrumb level to convert to Java level.</param>
 /// <returns>An Android Java object representing the SentryLevel.</returns>
 public static AndroidJavaObject ToJavaSentryLevel(this BreadcrumbLevel level)
 {
     using var javaSentryLevel = new AndroidJavaClass("io.sentry.SentryLevel");
     return(level switch
     {
         BreadcrumbLevel.Critical => javaSentryLevel.GetStatic <AndroidJavaObject>("FATAL"),
         BreadcrumbLevel.Error => javaSentryLevel.GetStatic <AndroidJavaObject>("ERROR"),
         BreadcrumbLevel.Warning => javaSentryLevel.GetStatic <AndroidJavaObject>("WARNING"),
         BreadcrumbLevel.Debug => javaSentryLevel.GetStatic <AndroidJavaObject>("DEBUG"),
         // BreadcrumbLevel.Info or unknown:
         _ => javaSentryLevel.GetStatic <AndroidJavaObject>("INFO")
     });
Esempio n. 17
0
 public void AddBreadcrumb(
     ISystemClock clock,
     string message,
     string type     = null,
     string category = null,
     IDictionary <string, string> data = null,
     BreadcrumbLevel level             = default)
 => SentryCore.AddBreadcrumb(
     clock: clock,
     message: message,
     type: type,
     data: data,
     category: category,
     level: level);
Esempio n. 18
0
 public static void AddBreadcrumb(
     this IHub hub,
     ISystemClock clock,
     string message,
     string category = null,
     string type     = null,
     IDictionary <string, string> data = null,
     BreadcrumbLevel level             = default)
 => hub.ConfigureScope(
     s => s.AddBreadcrumb(
         timestamp: (clock ?? SystemClock.Clock).GetUtcNow(),
         message: message,
         category: category,
         type: type, data: data?.ToImmutableDictionary(), level: level));
Esempio n. 19
0
 public static void AddBreadcrumb(
     this IHub hub,
     string message,
     string type,
     string category = null,
     IDictionary <string, string> data = null,
     BreadcrumbLevel level             = default)
 => hub.AddBreadcrumb(
     clock: null,
     message: message,
     type: type,
     data: data?.ToImmutableDictionary(),
     category: category,
     level: level);
Esempio n. 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Breadcrumb"/> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="type">The type.</param>
 /// <param name="data">The data.</param>
 /// <param name="category">The category.</param>
 /// <param name="level">The level.</param>
 public Breadcrumb(
     string message,
     string type,
     IImmutableDictionary <string, string> data = null,
     string category       = null,
     BreadcrumbLevel level = default)
     : this(
         DateTimeOffset.UtcNow,
         message,
         type,
         data,
         category,
         level)
 {
 }
Esempio n. 21
0
 public Breadcrumb(
     DateTimeOffset timestamp,
     string message = null,
     string type    = null,
     IImmutableDictionary <string, string> data = null,
     string category       = null,
     BreadcrumbLevel level = default)
 {
     Timestamp = timestamp;
     Message   = message;
     Type      = type;
     Data      = data;
     Category  = category;
     Level     = level;
 }
Esempio n. 22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Breadcrumb"/> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="type">The type.</param>
 /// <param name="data">The data.</param>
 /// <param name="category">The category.</param>
 /// <param name="level">The level.</param>
 public Breadcrumb(
     string message,
     string type,
     IReadOnlyDictionary <string, string> data = null,
     string category       = null,
     BreadcrumbLevel level = 0)
     : this(
         null,
         message,
         type,
         data,
         category,
         level)
 {
 }
Esempio n. 23
0
 internal Breadcrumb(
     DateTimeOffset?timestamp = null,
     string message           = null,
     string type = null,
     IReadOnlyDictionary <string, string> data = null,
     string category       = null,
     BreadcrumbLevel level = 0)
 {
     Timestamp = timestamp ?? DateTimeOffset.UtcNow;
     Message   = message;
     Type      = type;
     Data      = data;
     Category  = category;
     Level     = level;
 }
 public static void AddBreadcrumb(this Scope scope,
                                  DateTimeOffset?timestamp,
                                  string message,
                                  string category = null,
                                  string type     = null,
                                  IReadOnlyDictionary <string, string> data = null,
                                  BreadcrumbLevel level = 0)
 {
     scope.AddBreadcrumb(new Breadcrumb(
                             timestamp: timestamp,
                             message: message,
                             type: type,
                             data: data,
                             category: category,
                             level: level));
 }
Esempio n. 25
0
        /// <summary>
        /// Convert diagnostic data to JSON format
        /// </summary>
        /// <param name="id">Breadcrumbs id</param>
        /// <param name="message">breadcrumbs message</param>
        /// <param name="level">Breadcrumb level</param>
        /// <param name="type">Breadcrumb type</param>
        /// <param name="attributes">Breadcrumb attributes</param>
        /// <returns>JSON object</returns>
        private BacktraceJObject CreateBreadcrumbJson(
            double id,
            string message,
            BreadcrumbLevel level,
            UnityEngineLogLevel type,
            IDictionary <string, string> attributes)
        {
            var jsonObject = new BacktraceJObject();

            // breadcrumbs integration accepts timestamp in ms not in sec.
            jsonObject.Add("timestamp", DateTimeHelper.TimestampMs(), "F0");
            jsonObject.Add("id", id, "F0");
            jsonObject.Add("type", Enum.GetName(typeof(BreadcrumbLevel), level).ToLower());
            jsonObject.Add("level", Enum.GetName(typeof(UnityEngineLogLevel), type).ToLower());
            jsonObject.Add("message", message);
            if (attributes != null && attributes.Count > 0)
            {
                jsonObject.Add("attributes", new BacktraceJObject(attributes));
            }
            return(jsonObject);
        }
Esempio n. 26
0
        public void Emit_WithException_BreadcrumbFromException()
        {
            var expectedException = new Exception("expected message");
            const BreadcrumbLevel expectedLevel = BreadcrumbLevel.Critical;

            var sut = _fixture.GetSut();

            var evt = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Fatal, expectedException, MessageTemplate.Empty,
                                   Enumerable.Empty <LogEventProperty>());

            sut.Emit(evt);

            var b = _fixture.Scope.Breadcrumbs.First();

            Assert.Equal(b.Message, expectedException.Message);
            Assert.Equal(b.Timestamp, _fixture.Clock.GetUtcNow());
            Assert.Null(b.Category);
            Assert.Equal(b.Level, expectedLevel);
            Assert.Null(b.Type);
            Assert.Null(b.Data);
        }
        public void Log_WithOnlyException_GeneratesBreadcrumbFromException()
        {
            var expectedException = new Exception("expected message");

            const BreadcrumbLevel expectedLevel = BreadcrumbLevel.Error;

            var logger = _fixture.GetLogger(o => o.MinimumEventLevel = LogLevel.Fatal);

            logger.Error(expectedException);

            var b = _fixture.Scope.Breadcrumbs.First();

            Assert.Equal(b.Message, $"{expectedException.GetType()}: {expectedException.Message}");
            Assert.Equal(b.Timestamp, _fixture.Clock.GetUtcNow());
            Assert.Null(b.Category);
            Assert.Equal(b.Level, expectedLevel);
            Assert.Null(b.Type);
            Assert.NotNull(b.Data);
            Assert.Equal(expectedException.GetType().ToString(), b.Data["exception_type"]);
            Assert.Equal(expectedException.Message, b.Data["exception_message"]);
        }
Esempio n. 28
0
        /// <summary>
        /// Adds an automatic breadcrumb to the current scope if the previous one wasn't the same.
        /// </summary>
        /// <param name="hub">The Hub which holds the scope stack</param>
        /// <param name="options">The SentryXamarinOptions that holds the last breadcrumb info.</param>
        /// <param name="message">The message.</param>
        /// <param name="category">Category.</param>
        /// <param name="type">Breadcrumb type.</param>
        /// <param name="data">Additional data.</param>
        /// <param name="level">Breadcrumb level.</param>
        internal static void AddInternalBreadcrumb(
            this IHub hub,
            SentryXamarinOptions options,
            string message,
            string?category = null,
            string?type     = null,
            Dictionary <string, string>?data = null,
            BreadcrumbLevel level            = BreadcrumbLevel.Info)
        {
            var previousBreadcrumb = options.LastInternalBreadcrumb;

            //Filter duplicated internal breadcrumbs
            if (previousBreadcrumb != null &&
                previousBreadcrumb.Message == message &&
                previousBreadcrumb.Category == category &&
                previousBreadcrumb.Type == type &&
                previousBreadcrumb.Data?.Except(data).Any() is false &&
                DateTimeOffset.UtcNow.Subtract(previousBreadcrumb.Timestamp).TotalSeconds < options.InternalBreadcrumbDuplicationTimeSpan)
            {
                //Skip
                options.DiagnosticLogger?.Log(SentryLevel.Debug, DuplicatedBreadcrumbDropped);
            }
Esempio n. 29
0
        /// <summary>
        /// Adds a breadcrumb to the current scope.
        /// </summary>
        /// <param name="hub">The Hub which holds the scope stack.</param>
        /// <param name="message">The message.</param>
        /// <param name="category">Category.</param>
        /// <param name="type">Breadcrumb type.</param>
        /// <param name="data">Additional data.</param>
        /// <param name="level">Breadcrumb level.</param>
        public static void AddBreadcrumb(
            this IHub hub,
            string message,
            string?category = null,
            string?type     = null,
            IDictionary <string, string>?data = null,
            BreadcrumbLevel level             = default)
        {
            // Not to throw on code that ignores nullability warnings.
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (hub is null)
            {
                return;
            }

            hub.AddBreadcrumb(
                null,
                message,
                category,
                type,
                data != null ? new Dictionary <string, string>(data) : null,
                level);
        }
 internal static int GetBreadcrumbLevel(BreadcrumbLevel breadcrumbLevel) =>
 // https://github.com/getsentry/sentry-cocoa/blob/50f955aeb214601dd62b5dae7abdaddc8a1f24d9/Sources/Sentry/Public/SentryDefines.h#L99-L105
 breadcrumbLevel switch
 {