From 5892daca7243c3e4a8fe4035f08bc2366055e034 Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Thu, 15 Jul 2021 11:15:23 -0300 Subject: [PATCH 01/12] WIP --- Samples/Sample.Xamarin.Droid/MainActivity.cs | 3 +- .../SentryXamarinOptionsExtensions.cs | 11 +++ .../Screenshot/ScreenshotAttachment.droid.cs | 24 +++++++ .../Screenshot/ScreenshotAttachmentContent.cs | 23 ++++++ .../ScreenshotEventProcessor.droid.cs | 70 +++++++++++++++++++ Src/Sentry.Xamarin/Sentry.Xamarin.csproj | 2 +- Src/Sentry.Xamarin/SentryXamarin.cs | 1 + Src/Sentry.Xamarin/SentryXamarinOptions.cs | 16 +++-- 8 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs create mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs create mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs diff --git a/Samples/Sample.Xamarin.Droid/MainActivity.cs b/Samples/Sample.Xamarin.Droid/MainActivity.cs index 760a51d..c4e8925 100644 --- a/Samples/Sample.Xamarin.Droid/MainActivity.cs +++ b/Samples/Sample.Xamarin.Droid/MainActivity.cs @@ -19,9 +19,8 @@ protected override void OnCreate(Bundle savedInstanceState) { options.Dsn = "https://5a193123a9b841bc8d8e42531e7242a1@o447951.ingest.sentry.io/5560112"; options.AddXamarinFormsIntegration(); -#if DEBUG options.Debug = true; -#endif + options.AttachScreenshots = true; }); TabLayoutResource = Resource.Layout.Tabbar; diff --git a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs index 45cd7b7..9cc0c6a 100644 --- a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs +++ b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs @@ -2,6 +2,7 @@ using Xamarin.Essentials; using Sentry.Xamarin.Internals; using Sentry.Internals.Session; +using Sentry.Internals.Device.Screenshot; namespace Sentry { @@ -62,6 +63,16 @@ internal static void RegisterXamarinEventProcessors(this SentryXamarinOptions op #endif } + internal static void RegisterScreenshotEventProcessor(this SentryXamarinOptions options) + { +#if NATIVE_PROCESSOR && MONOANDROID9_0 + if (options.AttachScreenshots) + { + options.AddEventProcessor(new ScreenshotEventProcessor(options)); + } +#endif + } + internal static bool RegisterNativeIntegrations(this SentryXamarinOptions options) { if (options.NativeIntegrationEnabled) diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs new file mode 100644 index 0000000..0507f9e --- /dev/null +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs @@ -0,0 +1,24 @@ +namespace Sentry.Internals.Device.Screenshot +{ + internal class ScreenshotAttachment : Attachment + { + public ScreenshotAttachment(byte[] image) + : this( + AttachmentType.Default, + new ScreenshotAttachmentContent(image), + "screenshot", + "image/jpeg") + { + } + + private ScreenshotAttachment( + AttachmentType type, + IAttachmentContent content, + string fileName, + string? contentType) + : base(type, content, fileName, contentType) + { + } + } + +} diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs new file mode 100644 index 0000000..5c14e0f --- /dev/null +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Sentry.Internals.Device.Screenshot +{ + internal class ScreenshotAttachmentContent : IAttachmentContent + { + private MemoryStream _stream { get; } + + public ScreenshotAttachmentContent(byte[] image) => _stream = new MemoryStream(image); + + public Stream GetStream() + { + var stream = new MemoryStream(); + _stream.CopyTo(stream); + _stream.Seek(0, SeekOrigin.Begin); + stream.Seek(0, SeekOrigin.Begin); + return stream; + } + } +} \ No newline at end of file diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs new file mode 100644 index 0000000..89744c7 --- /dev/null +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs @@ -0,0 +1,70 @@ +using Sentry.Extensibility; +using Android.Graphics; +using Android.Views; +using Xamarin.Essentials; +using Android.App; +using System.IO; +using System.Threading.Tasks; + +namespace Sentry.Internals.Device.Screenshot +{ + internal class ScreenshotEventProcessor : ISentryEventProcessor + { + public SentryEvent? Process(SentryEvent @event) + { + var image = Capture(); + if (image != null) + { + SentrySdk.ConfigureScope(s => + { + s.AddAttachment(new ScreenshotAttachment(image)); + }); + } + return @event; + } + + private SentryOptions _options { get; } + + private Activity? _currentActivity => Platform.CurrentActivity; + + public ScreenshotEventProcessor(SentryOptions options) => _options = options; + + internal byte[] Capture() + { + + if (_currentActivity?.WindowManager.DefaultDisplay.Flags.HasFlag(DisplayFlags.Secure) == true) + { + _options.DiagnosticLogger?.LogWarning("Unable to take a screenshot of a secure window."); + } + + var view = _currentActivity?.Window?.DecorView?.RootView; + if (view == null) + { + _options.DiagnosticLogger?.LogWarning("Unable to find the main window."); + } + + var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888); + + using (var canvas = new Canvas(bitmap)) + { + var drawable = view.Background; + if (drawable != null) + drawable.Draw(canvas); + else + canvas.DrawColor(Color.White); + + view.Draw(canvas); + } + + //Perhaps opt if this code will compress or not since it could be performance heavy. + byte[] jpegData; + using (var stream = new MemoryStream()) + { + bitmap.Compress(Bitmap.CompressFormat.Jpeg, 30, stream); + jpegData = stream.ToArray(); + } + + return jpegData; + } + } +} diff --git a/Src/Sentry.Xamarin/Sentry.Xamarin.csproj b/Src/Sentry.Xamarin/Sentry.Xamarin.csproj index 4a21913..014ad6e 100644 --- a/Src/Sentry.Xamarin/Sentry.Xamarin.csproj +++ b/Src/Sentry.Xamarin/Sentry.Xamarin.csproj @@ -39,7 +39,7 @@ - + diff --git a/Src/Sentry.Xamarin/SentryXamarin.cs b/Src/Sentry.Xamarin/SentryXamarin.cs index 666cb24..db3f607 100644 --- a/Src/Sentry.Xamarin/SentryXamarin.cs +++ b/Src/Sentry.Xamarin/SentryXamarin.cs @@ -37,6 +37,7 @@ public static void Init(SentryXamarinOptions options) options.RegisterNativeActivityStatus(); options.RegisterNativeIntegrations(); options.RegisterXamarinEventProcessors(); + options.RegisterScreenshotEventProcessor(); options.RegisterXamarinInAppExclude(); options.ProtocolPackageName ??= ProtocolPackageName; SentrySdk.Init(options); diff --git a/Src/Sentry.Xamarin/SentryXamarinOptions.cs b/Src/Sentry.Xamarin/SentryXamarinOptions.cs index 7b51bcd..df66bcb 100644 --- a/Src/Sentry.Xamarin/SentryXamarinOptions.cs +++ b/Src/Sentry.Xamarin/SentryXamarinOptions.cs @@ -8,6 +8,17 @@ namespace Sentry /// public class SentryXamarinOptions : SentryOptions { + /// + /// Attaches screenshots from the app to events automatically whenever possible. + /// + public bool AttachScreenshots { get; set; } + + /// + /// Define the range of time that duplicated internal breadcrumbs will be ignored. + /// + public int InternalBreadcrumbDuplicationTimeSpan { get; set; } = 2; + + internal bool XamarinLoggerEnabled { get; set; } = true; internal bool NativeIntegrationEnabled { get; set; } = true; internal bool InternalCacheEnabled { get; set; } = true; @@ -22,10 +33,7 @@ public class SentryXamarinOptions : SentryOptions /// device's app status. /// internal IDeviceActiveLogger SessionLogger { get; set; } - /// - /// Define the range of time that duplicated internal breadcrumbs will be ignored. - /// - public int InternalBreadcrumbDuplicationTimeSpan { get; set; } = 2; + internal Breadcrumb LastInternalBreadcrumb {get;set;} /// From 974e0c3bccf44e73e3cf760e89aec7c53a4fa55d Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Fri, 16 Jul 2021 13:14:30 -0300 Subject: [PATCH 02/12] test --- .../Sample.Xamarin.Core.csproj | 2 +- .../Sample.Xamarin.Droid.csproj | 2 +- Samples/Sample.Xamarin.UWP/App.xaml.cs | 5 +- .../Sample.Xamarin.UWP/Package.appxmanifest | 4 +- .../Sample.Xamarin.UWP.csproj | 2 +- .../Sample.Xamarin.iOS.csproj | 4 +- .../SentryXamarinOptionsExtensions.cs | 2 +- ...hment.droid.cs => ScreenshotAttachment.cs} | 9 +- .../Screenshot/ScreenshotAttachmentContent.cs | 28 ++-- .../ScreenshotEventProcessor.droid.cs | 70 ---------- .../ScreenshotEventProcessor.droid.ios.cs | 38 ++++++ .../ScreenshotEventProcessor.uwp.cs | 121 ++++++++++++++++++ ...r.ios.cs => DeviceActiveLogger.ios.uwp.cs} | 0 .../Session/DeviceActiveLogger.uwp.cs | 11 -- Src/Sentry.Xamarin/Sentry.Xamarin.csproj | 28 ++-- Src/Sentry.Xamarin/SentryXamarinOptions.cs | 6 + 16 files changed, 211 insertions(+), 121 deletions(-) rename Src/Sentry.Xamarin/Internals/Device/Screenshot/{ScreenshotAttachment.droid.cs => ScreenshotAttachment.cs} (78%) delete mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs create mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs create mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs rename Src/Sentry.Xamarin/Internals/Session/{DeviceActiveLogger.ios.cs => DeviceActiveLogger.ios.uwp.cs} (100%) delete mode 100644 Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.uwp.cs diff --git a/Samples/Sample.Xamarin.Core/Sample.Xamarin.Core.csproj b/Samples/Sample.Xamarin.Core/Sample.Xamarin.Core.csproj index dfce5ed..f457355 100644 --- a/Samples/Sample.Xamarin.Core/Sample.Xamarin.Core.csproj +++ b/Samples/Sample.Xamarin.Core/Sample.Xamarin.Core.csproj @@ -15,7 +15,7 @@ - + diff --git a/Samples/Sample.Xamarin.Droid/Sample.Xamarin.Droid.csproj b/Samples/Sample.Xamarin.Droid/Sample.Xamarin.Droid.csproj index 9c6cdc0..4f354c6 100644 --- a/Samples/Sample.Xamarin.Droid/Sample.Xamarin.Droid.csproj +++ b/Samples/Sample.Xamarin.Droid/Sample.Xamarin.Droid.csproj @@ -67,7 +67,7 @@ 4.3.4 - + diff --git a/Samples/Sample.Xamarin.UWP/App.xaml.cs b/Samples/Sample.Xamarin.UWP/App.xaml.cs index c1a41c2..7207b16 100644 --- a/Samples/Sample.Xamarin.UWP/App.xaml.cs +++ b/Samples/Sample.Xamarin.UWP/App.xaml.cs @@ -1,4 +1,5 @@ using Sentry; +using Sentry.Infrastructure; using System; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; @@ -16,9 +17,9 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) { options.Dsn = "https://5a193123a9b841bc8d8e42531e7242a1@o447951.ingest.sentry.io/5560112"; options.AddXamarinFormsIntegration(); -#if DEBUG options.Debug = true; -#endif + options.DiagnosticLogger = new TraceDiagnosticLogger(SentryLevel.Debug); + options.AttachScreenshots = true; }); Frame rootFrame = Window.Current.Content as Frame; diff --git a/Samples/Sample.Xamarin.UWP/Package.appxmanifest b/Samples/Sample.Xamarin.UWP/Package.appxmanifest index dd692ba..eee947f 100644 --- a/Samples/Sample.Xamarin.UWP/Package.appxmanifest +++ b/Samples/Sample.Xamarin.UWP/Package.appxmanifest @@ -4,7 +4,8 @@ xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" - IgnorableNamespaces="uap mp"> + xmlns:uap6="http://schemas.microsoft.com/appx/manifest/uap/windows10/6" + IgnorableNamespaces="uap mp uap6"> + \ No newline at end of file diff --git a/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj b/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj index bdb5eef..59380b0 100644 --- a/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj +++ b/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj @@ -159,7 +159,7 @@ 2.0.0.9 - 1.5.3.2 + 1.6.1 4.8.0.1451 diff --git a/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj b/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj index 1664f4c..fbd17ce 100644 --- a/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj +++ b/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj @@ -132,7 +132,7 @@ 2.0.0.9 - + @@ -165,4 +165,4 @@ - + \ No newline at end of file diff --git a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs index 9cc0c6a..0348637 100644 --- a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs +++ b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs @@ -65,7 +65,7 @@ internal static void RegisterXamarinEventProcessors(this SentryXamarinOptions op internal static void RegisterScreenshotEventProcessor(this SentryXamarinOptions options) { -#if NATIVE_PROCESSOR && MONOANDROID9_0 +#if MONOANDROID9_0 || XAMARINIOS10 || UAP10_0_16299 if (options.AttachScreenshots) { options.AddEventProcessor(new ScreenshotEventProcessor(options)); diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs similarity index 78% rename from Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs rename to Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs index 0507f9e..6ae9f5b 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.droid.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs @@ -1,9 +1,11 @@ -namespace Sentry.Internals.Device.Screenshot +using System.IO; + +namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotAttachment : Attachment { - public ScreenshotAttachment(byte[] image) - : this( + public ScreenshotAttachment(Stream image) + : this( AttachmentType.Default, new ScreenshotAttachmentContent(image), "screenshot", @@ -20,5 +22,4 @@ private ScreenshotAttachment( { } } - } diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs index 5c14e0f..4085fbf 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs @@ -1,23 +1,25 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; +using System.IO; namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotAttachmentContent : IAttachmentContent { - private MemoryStream _stream { get; } + private byte[] _data { get; } - public ScreenshotAttachmentContent(byte[] image) => _stream = new MemoryStream(image); - - public Stream GetStream() + /// + /// The content ctor + /// + /// A Ready once stream containing the image data. + public ScreenshotAttachmentContent(Stream stream) { - var stream = new MemoryStream(); - _stream.CopyTo(stream); - _stream.Seek(0, SeekOrigin.Begin); - stream.Seek(0, SeekOrigin.Begin); - return stream; + _data = new byte[stream.Length]; + stream.Read(_data, 0, _data.Length); } + /// + /// Get a stream from the attachment data. + /// + /// A Memory stream containing the data. + public Stream GetStream() + => new MemoryStream(_data); } } \ No newline at end of file diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs deleted file mode 100644 index 89744c7..0000000 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.cs +++ /dev/null @@ -1,70 +0,0 @@ -using Sentry.Extensibility; -using Android.Graphics; -using Android.Views; -using Xamarin.Essentials; -using Android.App; -using System.IO; -using System.Threading.Tasks; - -namespace Sentry.Internals.Device.Screenshot -{ - internal class ScreenshotEventProcessor : ISentryEventProcessor - { - public SentryEvent? Process(SentryEvent @event) - { - var image = Capture(); - if (image != null) - { - SentrySdk.ConfigureScope(s => - { - s.AddAttachment(new ScreenshotAttachment(image)); - }); - } - return @event; - } - - private SentryOptions _options { get; } - - private Activity? _currentActivity => Platform.CurrentActivity; - - public ScreenshotEventProcessor(SentryOptions options) => _options = options; - - internal byte[] Capture() - { - - if (_currentActivity?.WindowManager.DefaultDisplay.Flags.HasFlag(DisplayFlags.Secure) == true) - { - _options.DiagnosticLogger?.LogWarning("Unable to take a screenshot of a secure window."); - } - - var view = _currentActivity?.Window?.DecorView?.RootView; - if (view == null) - { - _options.DiagnosticLogger?.LogWarning("Unable to find the main window."); - } - - var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888); - - using (var canvas = new Canvas(bitmap)) - { - var drawable = view.Background; - if (drawable != null) - drawable.Draw(canvas); - else - canvas.DrawColor(Color.White); - - view.Draw(canvas); - } - - //Perhaps opt if this code will compress or not since it could be performance heavy. - byte[] jpegData; - using (var stream = new MemoryStream()) - { - bitmap.Compress(Bitmap.CompressFormat.Jpeg, 30, stream); - jpegData = stream.ToArray(); - } - - return jpegData; - } - } -} diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs new file mode 100644 index 0000000..93b304b --- /dev/null +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -0,0 +1,38 @@ +using Sentry.Extensibility; +using Xamarin.Essentials; +using System.IO; +using System; + +namespace Sentry.Internals.Device.Screenshot +{ + internal class ScreenshotEventProcessor : ISentryEventProcessor + { + public SentryEvent? Process(SentryEvent @event) + { + try + { + var stream = Capture(); + if (stream != null) + { + SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); + //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); + } + } + catch (Exception ex) + { + _options.DiagnosticLogger?.LogError("Failed to capture a screenshot", ex); + } + return @event; + } + + private SentryXamarinOptions _options { get; } + + public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; + + internal Stream Capture() + { + var screenStream = global::Xamarin.Essentials.Screenshot.CaptureAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + return screenStream.OpenReadAsync(ScreenshotFormat.Jpeg).ConfigureAwait(false).GetAwaiter().GetResult(); + } + } +} diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs new file mode 100644 index 0000000..a6fb5e9 --- /dev/null +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs @@ -0,0 +1,121 @@ +#pragma warning disable CS0219 // Variable is assigned but its value is never used +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + +using Sentry.Extensibility; +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Windows.ApplicationModel.Core; +using Windows.UI.Core; +using Xamarin.Essentials; + +namespace Sentry.Internals.Device.Screenshot +{ + internal class ScreenshotEventProcessor : ISentryEventProcessor + { + public SentryEvent? Process(SentryEvent @event) + { + try + { + if (Capture() is { } stream) + { + //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); + SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); + } + } + catch (Exception ex) + { + _ = ex; + } + return @event; + } + + private SentryXamarinOptions _options { get; } + + public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; + + //BURN THIS CODE ☺ + public Stream? Capture() + { + var @lock2 = new ManualResetEvent(false); + var task2 = Task.Run(async () => + { + var task = new TaskCompletionSource(); + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => + { + try + { + //var screenshot = await global::Xamarin.Essentials.Screenshot.CaptureAsync().ConfigureAwait(false); + throw new Exception("eee"); + //task.SetResult(screenshot); + } + catch (Exception ex) + { + task.SetResult(ex); + } + }); + await task.Task.ConfigureAwait(false); + if (task.Task.Result is Exception ex) + { + _options.DiagnosticLogger?.LogError("Failed to capture a screenshot", ex); + } + else if (task.Task.Result is ScreenshotResult screenshot) + { + @lock2.Set(); + return screenshot; + } + @lock2.Set(); + return null; + }); + + @lock2.WaitOne(TimeSpan.FromSeconds(5)); + + if (task2.Result is { } screenshot) + { + return screenshot.OpenReadAsync(ScreenshotFormat.Jpeg).ConfigureAwait(false).GetAwaiter().GetResult(); + } + return null; + } + } +} +#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously +#pragma warning restore CS0219 // Variable is assigned but its value is never used + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.cs b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs similarity index 100% rename from Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.cs rename to Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs diff --git a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.uwp.cs b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.uwp.cs deleted file mode 100644 index b71257e..0000000 --- a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.uwp.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Sentry.Internals.Session -{ - internal class DeviceActiveLogger : IDeviceActiveLogger - { - /// - public void StatePaused() => SentrySdk.PauseSession(); - - /// - public void StateResumed() => SentrySdk.ResumeSession(); - } -} diff --git a/Src/Sentry.Xamarin/Sentry.Xamarin.csproj b/Src/Sentry.Xamarin/Sentry.Xamarin.csproj index 014ad6e..bddf03f 100644 --- a/Src/Sentry.Xamarin/Sentry.Xamarin.csproj +++ b/Src/Sentry.Xamarin/Sentry.Xamarin.csproj @@ -30,37 +30,37 @@ - - - - + + + + - - - - + + + + - + - + - + - + - + - + diff --git a/Src/Sentry.Xamarin/SentryXamarinOptions.cs b/Src/Sentry.Xamarin/SentryXamarinOptions.cs index df66bcb..945dcc7 100644 --- a/Src/Sentry.Xamarin/SentryXamarinOptions.cs +++ b/Src/Sentry.Xamarin/SentryXamarinOptions.cs @@ -11,8 +11,14 @@ public class SentryXamarinOptions : SentryOptions /// /// Attaches screenshots from the app to events automatically whenever possible. /// + /// + /// Pii information might be exposed by activating this feature. + /// public bool AttachScreenshots { get; set; } + //TODO: Maybe an option to choose between png and jpeg where jpeg is the default? + //public SentryScreenshotQuality ScreenshotQuality { get; set; } + /// /// Define the range of time that duplicated internal breadcrumbs will be ignored. /// From 2b46a4c250e87c480f16faf801be759ec261aede Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Fri, 16 Jul 2021 13:54:54 -0300 Subject: [PATCH 03/12] Small refactor (check if foreground) --- .../SentryXamarinOptionsExtensions.cs | 2 +- .../ScreenshotEventProcessor.droid.ios.cs | 18 ++++++++++------- .../ScreenshotEventProcessor.uwp.cs | 17 ++++++++++------ .../Session/DeviceActiveLogger.droid.cs | 7 +++++++ .../Session/DeviceActiveLogger.ios.uwp.cs | 20 +++++++++++++++++-- .../Internals/Session/IDeviceActiveLogger.cs | 2 ++ 6 files changed, 50 insertions(+), 16 deletions(-) diff --git a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs index 0348637..39120c7 100644 --- a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs +++ b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs @@ -65,7 +65,7 @@ internal static void RegisterXamarinEventProcessors(this SentryXamarinOptions op internal static void RegisterScreenshotEventProcessor(this SentryXamarinOptions options) { -#if MONOANDROID9_0 || XAMARINIOS10 || UAP10_0_16299 +#if NATIVE_PROCESSOR if (options.AttachScreenshots) { options.AddEventProcessor(new ScreenshotEventProcessor(options)); diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs index 93b304b..92ab44d 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -7,15 +7,22 @@ namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotEventProcessor : ISentryEventProcessor { + private SentryXamarinOptions _options { get; } + + public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; + public SentryEvent? Process(SentryEvent @event) { try { - var stream = Capture(); - if (stream != null) + if (_options.SessionLogger?.IsBackground() == false) { - SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); - //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); + var stream = Capture(); + if (stream != null) + { + SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); + //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); + } } } catch (Exception ex) @@ -25,9 +32,6 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor return @event; } - private SentryXamarinOptions _options { get; } - - public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; internal Stream Capture() { diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs index a6fb5e9..015f62b 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs @@ -14,14 +14,22 @@ namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotEventProcessor : ISentryEventProcessor { + private SentryXamarinOptions _options { get; } + + public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; + public SentryEvent? Process(SentryEvent @event) { try { - if (Capture() is { } stream) + if (_options.SessionLogger?.IsBackground() == false) { - //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); - SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); + + if (Capture() is { } stream) + { + //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); + SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); + } } } catch (Exception ex) @@ -31,9 +39,6 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor return @event; } - private SentryXamarinOptions _options { get; } - - public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; //BURN THIS CODE ☺ public Stream? Capture() diff --git a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs index 6e4d976..a4f899d 100644 --- a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs +++ b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs @@ -9,6 +9,13 @@ internal class DeviceActiveLogger : IDeviceActiveLogger /// private bool IsPaused; + /// + /// Informs if the device is on background. + /// + /// True if the app is on background, otherwise false + public bool IsBackground() => IsPaused; + + /// public void StatePaused() { diff --git a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs index b71257e..31cf219 100644 --- a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs +++ b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs @@ -2,10 +2,26 @@ { internal class DeviceActiveLogger : IDeviceActiveLogger { + private bool _isPaused { get; set; } = true; + + /// + /// Informs if the device is on background. + /// + /// True if the app is on background, otherwise false + public bool IsBackground() => _isPaused; + /// - public void StatePaused() => SentrySdk.PauseSession(); + public void StatePaused() + { + _isPaused = true; + SentrySdk.PauseSession(); + } /// - public void StateResumed() => SentrySdk.ResumeSession(); + public void StateResumed() + { + _isPaused = false; + SentrySdk.ResumeSession(); + } } } diff --git a/Src/Sentry.Xamarin/Internals/Session/IDeviceActiveLogger.cs b/Src/Sentry.Xamarin/Internals/Session/IDeviceActiveLogger.cs index 774569d..72bf24f 100644 --- a/Src/Sentry.Xamarin/Internals/Session/IDeviceActiveLogger.cs +++ b/Src/Sentry.Xamarin/Internals/Session/IDeviceActiveLogger.cs @@ -5,5 +5,7 @@ internal interface IDeviceActiveLogger void StatePaused(); void StateResumed(); + + bool IsBackground(); } } From ee080a3995ff46da6f17e3f253a58d4d7b33c6a5 Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Fri, 16 Jul 2021 14:28:35 -0300 Subject: [PATCH 04/12] rip lines --- .../ScreenshotEventProcessor.uwp.cs | 40 +------------------ 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs index 015f62b..c9ee011 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs @@ -85,42 +85,4 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio } } #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously -#pragma warning restore CS0219 // Variable is assigned but its value is never used - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +#pragma warning restore CS0219 // Variable is assigned but its value is never used \ No newline at end of file From 858f6d15796cb9575c3e41bbb932f75df2a600db Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Mon, 19 Jul 2021 09:26:31 -0300 Subject: [PATCH 05/12] wip --- Directory.Build.props | 2 +- .../Device/Screenshot/ScreenshotAttachment.cs | 7 ++--- .../Screenshot/ScreenshotAttachmentContent.cs | 27 +++++++++++++++++-- .../ScreenshotEventProcessor.droid.ios.cs | 17 +++++++++--- .../ScreenshotEventProcessor.uwp.cs | 12 +++++++-- Src/Sentry.Xamarin/SentryXamarinOptions.cs | 1 + 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 3fb696d..86f5fc0 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -9,7 +9,7 @@ - + diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs index 6ae9f5b..cd6bef6 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs @@ -1,13 +1,14 @@ -using System.IO; +using System; +using System.IO; namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotAttachment : Attachment { - public ScreenshotAttachment(Stream image) + public ScreenshotAttachment(ScreenshotAttachmentContent content) : this( AttachmentType.Default, - new ScreenshotAttachmentContent(image), + content, "screenshot", "image/jpeg") { diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs index 4085fbf..4933a97 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs @@ -4,22 +4,45 @@ namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotAttachmentContent : IAttachmentContent { - private byte[] _data { get; } + private byte[] _data { get; set; } + + private bool _wasRead{ get; set; } /// /// The content ctor /// /// A Ready once stream containing the image data. public ScreenshotAttachmentContent(Stream stream) + { + SetNewData(stream); + } + + public void SetNewData(Stream stream) { _data = new byte[stream.Length]; stream.Read(_data, 0, _data.Length); } + + /// + /// Inform is the contenet was previously read. + /// By calling this function you will reset the read state to false and return the current state. + /// + /// true if the content was read. + public bool GetWasRead() + { + var read = _wasRead; + _wasRead = false; + return read; + } + /// /// Get a stream from the attachment data. /// /// A Memory stream containing the data. public Stream GetStream() - => new MemoryStream(_data); + { + _wasRead = true; + return new MemoryStream(_data); + } } } \ No newline at end of file diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs index 92ab44d..aad9737 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -7,6 +7,7 @@ namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotEventProcessor : ISentryEventProcessor { + private ScreenshotAttachmentContent? _screenshot { get; set; } private SentryXamarinOptions _options { get; } public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; @@ -20,8 +21,19 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor var stream = Capture(); if (stream != null) { - SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); - //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); + /* Create a new attachment if no screenshot attachment was fond. + * If there's an attachment content but it wasnt read during the last event processing + * Assume it was cleared and create a new one. + */ + if(_screenshot?.GetWasRead() != true) + { + _screenshot = new ScreenshotAttachmentContent(stream); + SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(_screenshot))); + } + else + { + _screenshot.SetNewData(stream); + } } } } @@ -32,7 +44,6 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor return @event; } - internal Stream Capture() { var screenStream = global::Xamarin.Essentials.Screenshot.CaptureAsync().ConfigureAwait(false).GetAwaiter().GetResult(); diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs index c9ee011..a222dc5 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs @@ -14,6 +14,7 @@ namespace Sentry.Internals.Device.Screenshot { internal class ScreenshotEventProcessor : ISentryEventProcessor { + private ScreenshotAttachmentContent? _screenshot { get; set; } private SentryXamarinOptions _options { get; } public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; @@ -27,8 +28,15 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor if (Capture() is { } stream) { - //@event.Contexts["Sentry::Screenshot"] = new ScreenshotAttachment(stream); - SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(stream))); + if (_screenshot == null) + { + _screenshot = new ScreenshotAttachmentContent(stream); + SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(_screenshot))); + } + else + { + _screenshot.SetNewData(stream); + } } } } diff --git a/Src/Sentry.Xamarin/SentryXamarinOptions.cs b/Src/Sentry.Xamarin/SentryXamarinOptions.cs index 945dcc7..3b04f44 100644 --- a/Src/Sentry.Xamarin/SentryXamarinOptions.cs +++ b/Src/Sentry.Xamarin/SentryXamarinOptions.cs @@ -49,6 +49,7 @@ public SentryXamarinOptions() { IsEnvironmentUser = false; AutoSessionTracking = true; + IsGlobalModeEnabled = true; } } } From 142865be04f64e9288bc1942d3ca06bd101c75c3 Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Wed, 21 Jul 2021 15:34:02 -0300 Subject: [PATCH 06/12] a --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 86f5fc0..c36bf80 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -9,7 +9,7 @@ - + From 453423e7b5b8e30d5b1613d1bbd329796a317d87 Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Mon, 26 Jul 2021 14:10:03 -0300 Subject: [PATCH 07/12] Requested changes. --- .../Device/Screenshot/ScreenshotAttachmentContent.cs | 6 +++--- .../Screenshot/ScreenshotEventProcessor.droid.ios.cs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs index 4933a97..ba32bcf 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs @@ -24,11 +24,11 @@ public void SetNewData(Stream stream) } /// - /// Inform is the contenet was previously read. + /// Inform is the content was previously read. /// By calling this function you will reset the read state to false and return the current state. /// /// true if the content was read. - public bool GetWasRead() + public bool ResetWasRead() { var read = _wasRead; _wasRead = false; @@ -45,4 +45,4 @@ public Stream GetStream() return new MemoryStream(_data); } } -} \ No newline at end of file +} diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs index aad9737..8d2f500 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -19,13 +19,13 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor if (_options.SessionLogger?.IsBackground() == false) { var stream = Capture(); - if (stream != null) + if (stream is not null) { - /* Create a new attachment if no screenshot attachment was fond. + /* Create a new attachment if no screenshot attachment was found. * If there's an attachment content but it wasnt read during the last event processing * Assume it was cleared and create a new one. */ - if(_screenshot?.GetWasRead() != true) + if (_screenshot?.ResetWasRead() != true) { _screenshot = new ScreenshotAttachmentContent(stream); SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(_screenshot))); @@ -44,7 +44,7 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor return @event; } - internal Stream Capture() + private Stream Capture() { var screenStream = global::Xamarin.Essentials.Screenshot.CaptureAsync().ConfigureAwait(false).GetAwaiter().GetResult(); return screenStream.OpenReadAsync(ScreenshotFormat.Jpeg).ConfigureAwait(false).GetAwaiter().GetResult(); From e8a58f6bfb150a0fe87cfc1637cf20ff4b47541d Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Mon, 26 Jul 2021 16:07:37 -0300 Subject: [PATCH 08/12] test --- .../Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs index 8d2f500..3f3826a 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -19,7 +19,7 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor if (_options.SessionLogger?.IsBackground() == false) { var stream = Capture(); - if (stream is not null) + if (stream != null) { /* Create a new attachment if no screenshot attachment was found. * If there's an attachment content but it wasnt read during the last event processing From 45f5964794406c7fc79755ba53d6b8274c8d183f Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Mon, 26 Jul 2021 16:25:24 -0300 Subject: [PATCH 09/12] rollback --- .../Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs index 3f3826a..8d2f500 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -19,7 +19,7 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor if (_options.SessionLogger?.IsBackground() == false) { var stream = Capture(); - if (stream != null) + if (stream is not null) { /* Create a new attachment if no screenshot attachment was found. * If there's an attachment content but it wasnt read during the last event processing From 3e9e248d8305c93b90602667454df3522b21d24b Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Mon, 26 Jul 2021 16:33:13 -0300 Subject: [PATCH 10/12] test --- .../Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs index 8d2f500..3f3826a 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs @@ -19,7 +19,7 @@ internal class ScreenshotEventProcessor : ISentryEventProcessor if (_options.SessionLogger?.IsBackground() == false) { var stream = Capture(); - if (stream is not null) + if (stream != null) { /* Create a new attachment if no screenshot attachment was found. * If there's an attachment content but it wasnt read during the last event processing From 4235ec24f91e437d667645464971d8dbf0ac5f77 Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Wed, 28 Jul 2021 16:05:15 -0300 Subject: [PATCH 11/12] small cleanup (removed UWP code) --- .../Sample.Xamarin.UWP.csproj | 2 +- .../SentryXamarinOptionsExtensions.cs | 2 +- .../Screenshot/ScreenshotAttachmentContent.cs | 2 +- .../ScreenshotEventProcessor.uwp.cs | 96 ------------------- 4 files changed, 3 insertions(+), 99 deletions(-) delete mode 100644 Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs diff --git a/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj b/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj index 59380b0..963bc4c 100644 --- a/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj +++ b/Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj @@ -196,4 +196,4 @@ --> - \ No newline at end of file + diff --git a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs index 39120c7..2648f86 100644 --- a/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs +++ b/Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs @@ -65,7 +65,7 @@ internal static void RegisterXamarinEventProcessors(this SentryXamarinOptions op internal static void RegisterScreenshotEventProcessor(this SentryXamarinOptions options) { -#if NATIVE_PROCESSOR +#if NATIVE_PROCESSOR && !UAP10_0_16299 if (options.AttachScreenshots) { options.AddEventProcessor(new ScreenshotEventProcessor(options)); diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs index ba32bcf..db87381 100644 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs +++ b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs @@ -6,7 +6,7 @@ internal class ScreenshotAttachmentContent : IAttachmentContent { private byte[] _data { get; set; } - private bool _wasRead{ get; set; } + private bool _wasRead { get; set; } /// /// The content ctor diff --git a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs b/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs deleted file mode 100644 index a222dc5..0000000 --- a/Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.uwp.cs +++ /dev/null @@ -1,96 +0,0 @@ -#pragma warning disable CS0219 // Variable is assigned but its value is never used -#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously - -using Sentry.Extensibility; -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; -using Windows.ApplicationModel.Core; -using Windows.UI.Core; -using Xamarin.Essentials; - -namespace Sentry.Internals.Device.Screenshot -{ - internal class ScreenshotEventProcessor : ISentryEventProcessor - { - private ScreenshotAttachmentContent? _screenshot { get; set; } - private SentryXamarinOptions _options { get; } - - public ScreenshotEventProcessor(SentryXamarinOptions options) => _options = options; - - public SentryEvent? Process(SentryEvent @event) - { - try - { - if (_options.SessionLogger?.IsBackground() == false) - { - - if (Capture() is { } stream) - { - if (_screenshot == null) - { - _screenshot = new ScreenshotAttachmentContent(stream); - SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(_screenshot))); - } - else - { - _screenshot.SetNewData(stream); - } - } - } - } - catch (Exception ex) - { - _ = ex; - } - return @event; - } - - - //BURN THIS CODE ☺ - public Stream? Capture() - { - var @lock2 = new ManualResetEvent(false); - var task2 = Task.Run(async () => - { - var task = new TaskCompletionSource(); - await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => - { - try - { - //var screenshot = await global::Xamarin.Essentials.Screenshot.CaptureAsync().ConfigureAwait(false); - throw new Exception("eee"); - //task.SetResult(screenshot); - } - catch (Exception ex) - { - task.SetResult(ex); - } - }); - await task.Task.ConfigureAwait(false); - if (task.Task.Result is Exception ex) - { - _options.DiagnosticLogger?.LogError("Failed to capture a screenshot", ex); - } - else if (task.Task.Result is ScreenshotResult screenshot) - { - @lock2.Set(); - return screenshot; - } - @lock2.Set(); - return null; - }); - - @lock2.WaitOne(TimeSpan.FromSeconds(5)); - - if (task2.Result is { } screenshot) - { - return screenshot.OpenReadAsync(ScreenshotFormat.Jpeg).ConfigureAwait(false).GetAwaiter().GetResult(); - } - return null; - } - } -} -#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously -#pragma warning restore CS0219 // Variable is assigned but its value is never used \ No newline at end of file From d15414dc7d9c48b7c705ce35989d38f1658ef1c2 Mon Sep 17 00:00:00 2001 From: Lucas Zimerman Fraulob Date: Thu, 29 Jul 2021 15:38:21 -0300 Subject: [PATCH 12/12] small cleanup. --- Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj | 2 +- .../Internals/Session/DeviceActiveLogger.droid.cs | 1 - Src/Sentry.Xamarin/SentryXamarinOptions.cs | 4 ---- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj b/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj index fbd17ce..880715d 100644 --- a/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj +++ b/Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj @@ -165,4 +165,4 @@ - \ No newline at end of file + diff --git a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs index a4f899d..7c31543 100644 --- a/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs +++ b/Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.droid.cs @@ -15,7 +15,6 @@ internal class DeviceActiveLogger : IDeviceActiveLogger /// True if the app is on background, otherwise false public bool IsBackground() => IsPaused; - /// public void StatePaused() { diff --git a/Src/Sentry.Xamarin/SentryXamarinOptions.cs b/Src/Sentry.Xamarin/SentryXamarinOptions.cs index 3b04f44..ee36d5a 100644 --- a/Src/Sentry.Xamarin/SentryXamarinOptions.cs +++ b/Src/Sentry.Xamarin/SentryXamarinOptions.cs @@ -16,15 +16,11 @@ public class SentryXamarinOptions : SentryOptions /// public bool AttachScreenshots { get; set; } - //TODO: Maybe an option to choose between png and jpeg where jpeg is the default? - //public SentryScreenshotQuality ScreenshotQuality { get; set; } - /// /// Define the range of time that duplicated internal breadcrumbs will be ignored. /// public int InternalBreadcrumbDuplicationTimeSpan { get; set; } = 2; - internal bool XamarinLoggerEnabled { get; set; } = true; internal bool NativeIntegrationEnabled { get; set; } = true; internal bool InternalCacheEnabled { get; set; } = true;