|
| 1 | +namespace Sentry.Maui.Internal; |
| 2 | + |
| 3 | +internal static class Extensions |
| 4 | +{ |
| 5 | + public static void AddBreadcrumbForEvent(this IHub hub, |
| 6 | + object? sender, |
| 7 | + string eventName, |
| 8 | + string? type, |
| 9 | + string? category, |
| 10 | + Action<Dictionary<string, string>>? addExtraData) |
| 11 | + => hub.AddBreadcrumbForEvent(sender, eventName, type, category, default, addExtraData); |
| 12 | + |
| 13 | + public static void AddBreadcrumbForEvent(this IHub hub, |
| 14 | + object? sender, |
| 15 | + string eventName, |
| 16 | + string? type = null, |
| 17 | + string? category = null, |
| 18 | + BreadcrumbLevel level = default, |
| 19 | + Action<Dictionary<string, string>>? addExtraData = null) |
| 20 | + { |
| 21 | + var data = new Dictionary<string, string>(); |
| 22 | + if (sender is Element element) |
| 23 | + { |
| 24 | + data.AddElementInfo(element, null); |
| 25 | + } |
| 26 | + |
| 27 | + addExtraData?.Invoke(data); |
| 28 | + |
| 29 | + var message = sender != null ? $"{sender.GetType().Name}.{eventName}" : eventName; |
| 30 | + hub.AddBreadcrumb(message, category, type, data, level); |
| 31 | + } |
| 32 | + |
| 33 | + public static void AddElementInfo(this IDictionary<string, string> data, Element? element, string? property) |
| 34 | + { |
| 35 | + if (element is null) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + var typeName = element.GetType().Name; |
| 41 | + var prefix = (property ?? typeName) + "."; |
| 42 | + |
| 43 | + if (property != null) |
| 44 | + { |
| 45 | + data.Add(property, typeName); |
| 46 | + } |
| 47 | + |
| 48 | + // The element ID seems to be mostly useless noise |
| 49 | + //data.Add(prefix + nameof(element.Id), element.Id.ToString()); |
| 50 | + |
| 51 | + if (element.StyleId != null) |
| 52 | + { |
| 53 | + // The StyleId correlates to the element's name if one is set in XAML |
| 54 | + // TODO: Is there a better way to get this? |
| 55 | + data.Add(prefix + "Name", element.StyleId); |
| 56 | + } |
| 57 | + |
| 58 | + if (element is ITitledElement { Title: { } } titledElement) |
| 59 | + { |
| 60 | + // TODO: Scrub PII ? |
| 61 | + data.Add(prefix + nameof(titledElement.Title), titledElement.Title); |
| 62 | + } |
| 63 | + |
| 64 | + if (element is IText { Text: { } } textElement) |
| 65 | + { |
| 66 | + // TODO: Scrub PII ? |
| 67 | + data.Add(prefix + nameof(textElement.Text), textElement.Text); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments