|
| 1 | +using Exceptionless.Logging; |
| 2 | +using Microsoft.Maui.Controls.Shapes; |
| 3 | + |
| 4 | +namespace Exceptionless.SampleMaui; |
| 5 | + |
| 6 | +public sealed class MainPage : ContentPage { |
| 7 | + private readonly ExceptionlessClient _exceptionlessClient; |
| 8 | + private readonly Label _statusLabel; |
| 9 | + private readonly Label _lastReferenceIdLabel; |
| 10 | + private readonly ActivityIndicator _activityIndicator; |
| 11 | + |
| 12 | + public MainPage(ExceptionlessClient exceptionlessClient) { |
| 13 | + _exceptionlessClient = exceptionlessClient; |
| 14 | + |
| 15 | + Title = "Exceptionless"; |
| 16 | + BackgroundColor = Color.FromArgb("#F6F8FA"); |
| 17 | + |
| 18 | + _statusLabel = new Label { |
| 19 | + Text = "Ready", |
| 20 | + FontSize = 14, |
| 21 | + TextColor = Color.FromArgb("#314256"), |
| 22 | + LineBreakMode = LineBreakMode.WordWrap |
| 23 | + }; |
| 24 | + |
| 25 | + _lastReferenceIdLabel = new Label { |
| 26 | + Text = "Last reference id: none", |
| 27 | + FontSize = 13, |
| 28 | + TextColor = Color.FromArgb("#576575"), |
| 29 | + LineBreakMode = LineBreakMode.TailTruncation |
| 30 | + }; |
| 31 | + |
| 32 | + _activityIndicator = new ActivityIndicator { |
| 33 | + IsVisible = false, |
| 34 | + Color = Color.FromArgb("#276749") |
| 35 | + }; |
| 36 | + |
| 37 | + Content = BuildContent(); |
| 38 | + } |
| 39 | + |
| 40 | + private View BuildContent() { |
| 41 | + var sendExceptionButton = CreateActionButton("Send Handled Exception", OnSendExceptionClicked); |
| 42 | + var sendLogButton = CreateActionButton("Send Warning Log", OnSendLogClicked); |
| 43 | + var trackFeatureButton = CreateActionButton("Track Feature", OnTrackFeatureClicked); |
| 44 | + var flushButton = CreateActionButton("Flush Queue", OnFlushClicked); |
| 45 | + |
| 46 | + return new ScrollView { |
| 47 | + Content = new VerticalStackLayout { |
| 48 | + Padding = new Thickness(24, 28), |
| 49 | + Spacing = 18, |
| 50 | + Children = { |
| 51 | + new Label { |
| 52 | + Text = "Exceptionless MAUI Sample", |
| 53 | + FontSize = 26, |
| 54 | + FontAttributes = FontAttributes.Bold, |
| 55 | + TextColor = Color.FromArgb("#1D2733") |
| 56 | + }, |
| 57 | + new Label { |
| 58 | + Text = "Submit sample events through the core Exceptionless client.", |
| 59 | + FontSize = 15, |
| 60 | + TextColor = Color.FromArgb("#576575"), |
| 61 | + LineBreakMode = LineBreakMode.WordWrap |
| 62 | + }, |
| 63 | + new Border { |
| 64 | + Stroke = Color.FromArgb("#D8DEE6"), |
| 65 | + StrokeThickness = 1, |
| 66 | + BackgroundColor = Colors.White, |
| 67 | + StrokeShape = new RoundRectangle { CornerRadius = 8 }, |
| 68 | + Padding = new Thickness(18), |
| 69 | + Content = new VerticalStackLayout { |
| 70 | + Spacing = 12, |
| 71 | + Children = { |
| 72 | + new Label { |
| 73 | + Text = "Client", |
| 74 | + FontSize = 18, |
| 75 | + FontAttributes = FontAttributes.Bold, |
| 76 | + TextColor = Color.FromArgb("#1D2733") |
| 77 | + }, |
| 78 | + new Label { |
| 79 | + Text = $"Server: {_exceptionlessClient.Configuration.ServerUrl}", |
| 80 | + FontSize = 13, |
| 81 | + TextColor = Color.FromArgb("#576575"), |
| 82 | + LineBreakMode = LineBreakMode.TailTruncation |
| 83 | + }, |
| 84 | + new Label { |
| 85 | + Text = $"Private information: {_exceptionlessClient.Configuration.IncludePrivateInformation}", |
| 86 | + FontSize = 13, |
| 87 | + TextColor = Color.FromArgb("#576575") |
| 88 | + }, |
| 89 | + _statusLabel, |
| 90 | + _lastReferenceIdLabel, |
| 91 | + _activityIndicator |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + new Grid { |
| 96 | + ColumnDefinitions = { |
| 97 | + new ColumnDefinition { Width = GridLength.Star }, |
| 98 | + new ColumnDefinition { Width = GridLength.Star } |
| 99 | + }, |
| 100 | + RowDefinitions = { |
| 101 | + new RowDefinition { Height = GridLength.Auto }, |
| 102 | + new RowDefinition { Height = GridLength.Auto } |
| 103 | + }, |
| 104 | + ColumnSpacing = 12, |
| 105 | + RowSpacing = 12, |
| 106 | + Children = { |
| 107 | + sendExceptionButton, |
| 108 | + sendLogButton, |
| 109 | + trackFeatureButton, |
| 110 | + flushButton |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + private static Button CreateActionButton(string text, EventHandler clicked) { |
| 119 | + var button = new Button { |
| 120 | + Text = text, |
| 121 | + BackgroundColor = Color.FromArgb("#285A84"), |
| 122 | + TextColor = Colors.White, |
| 123 | + CornerRadius = 8, |
| 124 | + FontAttributes = FontAttributes.Bold, |
| 125 | + MinimumHeightRequest = 48 |
| 126 | + }; |
| 127 | + |
| 128 | + button.Clicked += clicked; |
| 129 | + return button; |
| 130 | + } |
| 131 | + |
| 132 | + private async void OnSendExceptionClicked(object? sender, EventArgs e) { |
| 133 | + await RunClientActionAsync("Handled exception queued.", () => { |
| 134 | + string referenceId = Guid.NewGuid().ToString("N"); |
| 135 | + |
| 136 | + try { |
| 137 | + throw new InvalidOperationException("Exceptionless MAUI sample handled exception."); |
| 138 | + } catch (Exception ex) { |
| 139 | + ex.ToExceptionless(_exceptionlessClient) |
| 140 | + .SetReferenceId(referenceId) |
| 141 | + .AddTags("handled") |
| 142 | + .SetProperty("Screen", nameof(MainPage)) |
| 143 | + .Submit(); |
| 144 | + } |
| 145 | + |
| 146 | + SetLastReferenceId(referenceId); |
| 147 | + return Task.CompletedTask; |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + private async void OnSendLogClicked(object? sender, EventArgs e) { |
| 152 | + await RunClientActionAsync("Warning log queued.", () => { |
| 153 | + _exceptionlessClient.SubmitLog("Exceptionless.SampleMaui.MainPage", "MAUI sample warning log.", LogLevel.Warn); |
| 154 | + SetLastReferenceId(_exceptionlessClient.GetLastReferenceId()); |
| 155 | + return Task.CompletedTask; |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + private async void OnTrackFeatureClicked(object? sender, EventArgs e) { |
| 160 | + await RunClientActionAsync("Feature usage queued.", () => { |
| 161 | + _exceptionlessClient.SubmitFeatureUsage("MauiSample.TrackFeature"); |
| 162 | + SetLastReferenceId(_exceptionlessClient.GetLastReferenceId()); |
| 163 | + return Task.CompletedTask; |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + private async void OnFlushClicked(object? sender, EventArgs e) { |
| 168 | + await RunClientActionAsync("Queue processed.", () => _exceptionlessClient.ProcessQueueAsync()); |
| 169 | + } |
| 170 | + |
| 171 | + private async Task RunClientActionAsync(string successMessage, Func<Task> action) { |
| 172 | + try { |
| 173 | + _activityIndicator.IsVisible = true; |
| 174 | + _activityIndicator.IsRunning = true; |
| 175 | + _statusLabel.Text = "Working..."; |
| 176 | + |
| 177 | + await action(); |
| 178 | + |
| 179 | + _statusLabel.Text = successMessage; |
| 180 | + } catch (Exception ex) { |
| 181 | + _statusLabel.Text = $"Error: {ex.Message}"; |
| 182 | + } finally { |
| 183 | + _activityIndicator.IsRunning = false; |
| 184 | + _activityIndicator.IsVisible = false; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private void SetLastReferenceId(string? referenceId) { |
| 189 | + _lastReferenceIdLabel.Text = String.IsNullOrEmpty(referenceId) |
| 190 | + ? "Last reference id: none" |
| 191 | + : $"Last reference id: {referenceId}"; |
| 192 | + } |
| 193 | +} |
0 commit comments