Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 411be60

Browse files
feat: Add Screenshots automatically. (#76)
1 parent f280fc6 commit 411be60

19 files changed

+216
-51
lines changed

Samples/Sample.Xamarin.Core/Sample.Xamarin.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="Rg.Plugins.Popup" Version="2.0.0.9" />
1616
<PackageReference Include="Xamanimation" Version="1.3.0" />
1717
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
18-
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
18+
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

Samples/Sample.Xamarin.Droid/MainActivity.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ protected override void OnCreate(Bundle savedInstanceState)
1919
{
2020
options.Dsn = "https://[email protected]/5560112";
2121
options.AddXamarinFormsIntegration();
22-
#if DEBUG
2322
options.Debug = true;
24-
#endif
23+
options.AttachScreenshots = true;
2524
});
2625

2726
TabLayoutResource = Resource.Layout.Tabbar;

Samples/Sample.Xamarin.Droid/Sample.Xamarin.Droid.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<Version>4.3.4</Version>
6868
</PackageReference>
6969
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
70-
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
70+
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="Dependencies\NativeCrash.cs" />

Samples/Sample.Xamarin.UWP/App.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Sentry;
2+
using Sentry.Infrastructure;
23
using System;
34
using Windows.ApplicationModel;
45
using Windows.ApplicationModel.Activation;
@@ -16,9 +17,9 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
1617
{
1718
options.Dsn = "https://[email protected]/5560112";
1819
options.AddXamarinFormsIntegration();
19-
#if DEBUG
2020
options.Debug = true;
21-
#endif
21+
options.DiagnosticLogger = new TraceDiagnosticLogger(SentryLevel.Debug);
22+
options.AttachScreenshots = true;
2223
});
2324
Frame rootFrame = Window.Current.Content as Frame;
2425

Samples/Sample.Xamarin.UWP/Package.appxmanifest

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
55
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
66
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
7-
IgnorableNamespaces="uap mp">
7+
xmlns:uap6="http://schemas.microsoft.com/appx/manifest/uap/windows10/6"
8+
IgnorableNamespaces="uap mp uap6">
89

910
<Identity
1011
Name="com.companyname.Sample.Xamarin.UWP"
@@ -45,5 +46,6 @@
4546

4647
<Capabilities>
4748
<Capability Name="internetClient" />
49+
<uap6:Capability Name="graphicsCapture"/>
4850
</Capabilities>
4951
</Package>

Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
<Version>2.0.0.9</Version>
160160
</PackageReference>
161161
<PackageReference Include="Xamarin.Essentials">
162-
<Version>1.5.3.2</Version>
162+
<Version>1.6.1</Version>
163163
</PackageReference>
164164
<PackageReference Include="Xamarin.Forms">
165165
<Version>4.8.0.1451</Version>
@@ -196,4 +196,4 @@
196196
<Target Name="AfterBuild">
197197
</Target>
198198
-->
199-
</Project>
199+
</Project>

Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
<Version>2.0.0.9</Version>
133133
</PackageReference>
134134
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
135-
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
135+
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
136136
<!-- Required to work around an issue with Mono and transient dependencies that require this type -->
137137
<PackageReference Include="System.Memory" Version="4.5.4" IncludeAssets="None" />
138138
</ItemGroup>

Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Xamarin.Essentials;
33
using Sentry.Xamarin.Internals;
44
using Sentry.Internals.Session;
5+
using Sentry.Internals.Device.Screenshot;
56

67
namespace Sentry
78
{
@@ -62,6 +63,16 @@ internal static void RegisterXamarinEventProcessors(this SentryXamarinOptions op
6263
#endif
6364
}
6465

66+
internal static void RegisterScreenshotEventProcessor(this SentryXamarinOptions options)
67+
{
68+
#if NATIVE_PROCESSOR && !UAP10_0_16299
69+
if (options.AttachScreenshots)
70+
{
71+
options.AddEventProcessor(new ScreenshotEventProcessor(options));
72+
}
73+
#endif
74+
}
75+
6576
internal static bool RegisterNativeIntegrations(this SentryXamarinOptions options)
6677
{
6778
if (options.NativeIntegrationEnabled)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Sentry.Internals.Device.Screenshot
5+
{
6+
internal class ScreenshotAttachment : Attachment
7+
{
8+
public ScreenshotAttachment(ScreenshotAttachmentContent content)
9+
: this(
10+
AttachmentType.Default,
11+
content,
12+
"screenshot",
13+
"image/jpeg")
14+
{
15+
}
16+
17+
private ScreenshotAttachment(
18+
AttachmentType type,
19+
IAttachmentContent content,
20+
string fileName,
21+
string? contentType)
22+
: base(type, content, fileName, contentType)
23+
{
24+
}
25+
}
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.IO;
2+
3+
namespace Sentry.Internals.Device.Screenshot
4+
{
5+
internal class ScreenshotAttachmentContent : IAttachmentContent
6+
{
7+
private byte[] _data { get; set; }
8+
9+
private bool _wasRead { get; set; }
10+
11+
/// <summary>
12+
/// The content ctor
13+
/// </summary>
14+
/// <param name="stream">A Ready once stream containing the image data.</param>
15+
public ScreenshotAttachmentContent(Stream stream)
16+
{
17+
SetNewData(stream);
18+
}
19+
20+
public void SetNewData(Stream stream)
21+
{
22+
_data = new byte[stream.Length];
23+
stream.Read(_data, 0, _data.Length);
24+
}
25+
26+
/// <summary>
27+
/// Inform is the content was previously read.
28+
/// By calling this function you will reset the read state to false and return the current state.
29+
/// </summary>
30+
/// <returns>true if the content was read.</returns>
31+
public bool ResetWasRead()
32+
{
33+
var read = _wasRead;
34+
_wasRead = false;
35+
return read;
36+
}
37+
38+
/// <summary>
39+
/// Get a stream from the attachment data.
40+
/// </summary>
41+
/// <returns>A Memory stream containing the data.</returns>
42+
public Stream GetStream()
43+
{
44+
_wasRead = true;
45+
return new MemoryStream(_data);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)