This repository was archived by the owner on Nov 7, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Add Screenshots automatically. #76
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5892dac
WIP
lucas-zimerman 974e0c3
test
lucas-zimerman 2b46a4c
Small refactor (check if foreground)
lucas-zimerman ee080a3
rip lines
lucas-zimerman 858f6d1
wip
lucas-zimerman 142865b
a
lucas-zimerman 453423e
Requested changes.
lucas-zimerman e8a58f6
test
lucas-zimerman 45f5964
rollback
lucas-zimerman 3e9e248
test
lucas-zimerman 4235ec2
small cleanup (removed UWP code)
lucas-zimerman d15414d
small cleanup.
lucas-zimerman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,8 @@ protected override void OnCreate(Bundle savedInstanceState) | |
| { | ||
| options.Dsn = "https://[email protected]/5560112"; | ||
| options.AddXamarinFormsIntegration(); | ||
| #if DEBUG | ||
| options.Debug = true; | ||
| #endif | ||
| options.AttachScreenshots = true; | ||
| }); | ||
|
|
||
| TabLayoutResource = Resource.Layout.Tabbar; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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://[email protected]/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; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachment.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace Sentry.Internals.Device.Screenshot | ||
| { | ||
| internal class ScreenshotAttachment : Attachment | ||
| { | ||
| public ScreenshotAttachment(ScreenshotAttachmentContent content) | ||
| : this( | ||
| AttachmentType.Default, | ||
| content, | ||
| "screenshot", | ||
| "image/jpeg") | ||
| { | ||
| } | ||
|
|
||
| private ScreenshotAttachment( | ||
| AttachmentType type, | ||
| IAttachmentContent content, | ||
| string fileName, | ||
| string? contentType) | ||
| : base(type, content, fileName, contentType) | ||
| { | ||
| } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotAttachmentContent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using System.IO; | ||
|
|
||
| namespace Sentry.Internals.Device.Screenshot | ||
| { | ||
| internal class ScreenshotAttachmentContent : IAttachmentContent | ||
| { | ||
| private byte[] _data { get; set; } | ||
|
|
||
| private bool _wasRead { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The content ctor | ||
| /// </summary> | ||
| /// <param name="stream">A Ready once stream containing the image data.</param> | ||
| public ScreenshotAttachmentContent(Stream stream) | ||
| { | ||
| SetNewData(stream); | ||
| } | ||
|
|
||
| public void SetNewData(Stream stream) | ||
| { | ||
| _data = new byte[stream.Length]; | ||
| stream.Read(_data, 0, _data.Length); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Inform is the content was previously read. | ||
| /// By calling this function you will reset the read state to false and return the current state. | ||
| /// </summary> | ||
| /// <returns>true if the content was read.</returns> | ||
| public bool ResetWasRead() | ||
| { | ||
| var read = _wasRead; | ||
| _wasRead = false; | ||
| return read; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get a stream from the attachment data. | ||
| /// </summary> | ||
| /// <returns>A Memory stream containing the data.</returns> | ||
| public Stream GetStream() | ||
| { | ||
| _wasRead = true; | ||
| return new MemoryStream(_data); | ||
| } | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
Src/Sentry.Xamarin/Internals/Device/Screenshot/ScreenshotEventProcessor.droid.ios.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using Sentry.Extensibility; | ||
| using Xamarin.Essentials; | ||
| using System.IO; | ||
| using System; | ||
|
|
||
| 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) | ||
| { | ||
| var stream = Capture(); | ||
| 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 | ||
| * Assume it was cleared and create a new one. | ||
| */ | ||
| if (_screenshot?.ResetWasRead() != true) | ||
| { | ||
| _screenshot = new ScreenshotAttachmentContent(stream); | ||
| SentrySdk.ConfigureScope(s => s.AddAttachment(new ScreenshotAttachment(_screenshot))); | ||
| } | ||
| else | ||
| { | ||
| _screenshot.SetNewData(stream); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _options.DiagnosticLogger?.LogError("Failed to capture a screenshot", ex); | ||
| } | ||
| return @event; | ||
| } | ||
|
|
||
| private Stream Capture() | ||
| { | ||
| var screenStream = global::Xamarin.Essentials.Screenshot.CaptureAsync().ConfigureAwait(false).GetAwaiter().GetResult(); | ||
| return screenStream.OpenReadAsync(ScreenshotFormat.Jpeg).ConfigureAwait(false).GetAwaiter().GetResult(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.cs
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| namespace Sentry.Internals.Session | ||
| { | ||
| internal class DeviceActiveLogger : IDeviceActiveLogger | ||
| { | ||
| private bool _isPaused { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Informs if the device is on background. | ||
| /// </summary> | ||
| /// <returns>True if the app is on background, otherwise false</returns> | ||
| public bool IsBackground() => _isPaused; | ||
|
|
||
| /// <inheritdoc cref="IHub.PauseSession"/> | ||
| public void StatePaused() | ||
| { | ||
| _isPaused = true; | ||
| SentrySdk.PauseSession(); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IHub.ResumeSession"/> | ||
| public void StateResumed() | ||
| { | ||
| _isPaused = false; | ||
| SentrySdk.ResumeSession(); | ||
| } | ||
| } | ||
| } |
11 changes: 0 additions & 11 deletions
11
Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.uwp.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,7 @@ internal interface IDeviceActiveLogger | |
| void StatePaused(); | ||
|
|
||
| void StateResumed(); | ||
|
|
||
| bool IsBackground(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.