Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Sentry" Version="3.8.0" />
<PackageReference Include="Sentry" Version="3.8.2" />
<PackageReference Include="Roslynator.Analyzers" Version="3.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.1" PrivateAssets="All" />
Expand Down
2 changes: 1 addition & 1 deletion Samples/Sample.Xamarin.Core/Sample.Xamarin.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Rg.Plugins.Popup" Version="2.0.0.9" />
<PackageReference Include="Xamanimation" Version="1.3.0" />
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions Samples/Sample.Xamarin.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Samples/Sample.Xamarin.Droid/Sample.Xamarin.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<Version>4.3.4</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="Dependencies\NativeCrash.cs" />
Expand Down
5 changes: 3 additions & 2 deletions Samples/Sample.Xamarin.UWP/App.xaml.cs
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;
Expand All @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion Samples/Sample.Xamarin.UWP/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<Identity
Name="com.companyname.Sample.Xamarin.UWP"
Expand Down Expand Up @@ -45,5 +46,6 @@

<Capabilities>
<Capability Name="internetClient" />
<uap6:Capability Name="graphicsCapture"/>
</Capabilities>
</Package>
4 changes: 2 additions & 2 deletions Samples/Sample.Xamarin.UWP/Sample.Xamarin.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
<Version>2.0.0.9</Version>
</PackageReference>
<PackageReference Include="Xamarin.Essentials">
<Version>1.5.3.2</Version>
<Version>1.6.1</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms">
<Version>4.8.0.1451</Version>
Expand Down Expand Up @@ -196,4 +196,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
2 changes: 1 addition & 1 deletion Samples/Sample.Xamarin.iOS/Sample.Xamarin.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<Version>2.0.0.9</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
<!-- Required to work around an issue with Mono and transient dependencies that require this type -->
<PackageReference Include="System.Memory" Version="4.5.4" IncludeAssets="None" />
</ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions Src/Sentry.Xamarin/Extensions/SentryXamarinOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Xamarin.Essentials;
using Sentry.Xamarin.Internals;
using Sentry.Internals.Session;
using Sentry.Internals.Device.Screenshot;

namespace Sentry
{
Expand Down Expand Up @@ -62,6 +63,16 @@ internal static void RegisterXamarinEventProcessors(this SentryXamarinOptions op
#endif
}

internal static void RegisterScreenshotEventProcessor(this SentryXamarinOptions options)
{
#if NATIVE_PROCESSOR && !UAP10_0_16299
if (options.AttachScreenshots)
{
options.AddEventProcessor(new ScreenshotEventProcessor(options));
}
#endif
}

internal static bool RegisterNativeIntegrations(this SentryXamarinOptions options)
{
if (options.NativeIntegrationEnabled)
Expand Down
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)
{
}
}
}
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);
}
}
}
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ internal class DeviceActiveLogger : IDeviceActiveLogger
/// </summary>
private bool IsPaused;

/// <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()
{
Expand Down
11 changes: 0 additions & 11 deletions Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.cs

This file was deleted.

27 changes: 27 additions & 0 deletions Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.ios.uwp.cs
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 Src/Sentry.Xamarin/Internals/Session/DeviceActiveLogger.uwp.cs

This file was deleted.

2 changes: 2 additions & 0 deletions Src/Sentry.Xamarin/Internals/Session/IDeviceActiveLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ internal interface IDeviceActiveLogger
void StatePaused();

void StateResumed();

bool IsBackground();
}
}
30 changes: 15 additions & 15 deletions Src/Sentry.Xamarin/Sentry.Xamarin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="Internals\**\*.netstandard.cs" />
<Compile Remove="Internals\**\*.ios.cs" />
<Compile Remove="Internals\**\*.droid.cs" />
<Compile Remove="Internals\**\*.uwp.cs" />
<Compile Remove="Internals\**\*.netstandard*.cs" />
<Compile Remove="Internals\**\*.ios*.cs" />
<Compile Remove="Internals\**\*.droid*.cs" />
<Compile Remove="Internals\**\*.uwp*.cs" />

<None Include="Internals\**\*.netstandard*.cs" />
<None Include="Internals\**\*.ios*.cs" />
<None Include="Internals\**\*.droid*.cs" />
<None Include="Internals\**\*.uwp*.cs" />
</ItemGroup>

<None Include="Internals\**\*.netstandard.cs" />
<None Include="Internals\**\*.ios.cs" />
<None Include="Internals\**\*.droid.cs" />
<None Include="Internals\**\*.uwp.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Essentials" Version="1.4.0" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarinios')) ">
<Compile Include="Internals\**\*.ios.cs" />
<Compile Include="Internals\**\*.ios*.cs" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('monoandroid')) ">
<Compile Include="Internals\**\*.droid.cs" />
<Compile Include="Internals\**\*.droid*.cs" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('uap'))">
<Compile Include="Internals\**\*.uwp.cs" />
<Compile Include="Internals\**\*.uwp*.cs" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.9" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<Compile Include="Internals\**\*.netstandard.cs" />
<Compile Include="Internals\**\*.netstandard*.cs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Src/Sentry.Xamarin/SentryXamarin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading