Skip to content

Commit

Permalink
Feat: Allow logcat attachments to be previewed in Sentry (#3711)
Browse files Browse the repository at this point in the history
  • Loading branch information
bricefriha authored Oct 31, 2024
1 parent 0b13b7a commit 44d0c69
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Android - allow logcat attachments to be previewed in Sentry ([#3711](https://github.com/getsentry/sentry-dotnet/pull/3711))
- Added a `SetBeforeScreenshotCapture` callback to the options: allowing the user to set an action before the screenshot is taken ([#3661](https://github.com/getsentry/sentry-dotnet/pull/3661))
- Make `Sentry.AspNetCore.Blazor.WebAssembly` generally available. ([#3674](https://github.com/getsentry/sentry-dotnet/pull/3674))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public SentryEvent Process(SentryEvent @event, SentryHint hint)
output.Seek(0, SeekOrigin.Begin);
var bytes = output.ToArray();

hint.Attachments.Add(new SentryAttachment(AttachmentType.Default, new ByteAttachmentContent(bytes), "logcat.log", "text/logcat"));
hint.Attachments.Add(new SentryAttachment(AttachmentType.Default, new ByteAttachmentContent(bytes), "logcat.log", "text/plain"));

//hint.AddAttachment($"{filesDir.Path}/{fileName}", AttachmentType.Default, "text/logcat");

Expand Down
67 changes: 67 additions & 0 deletions test/Sentry.Maui.Tests/SentryMauiLogcatsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.Extensions.Options;
#if ANDROID
using Sentry.Android;
#endif

namespace Sentry.Maui.Tests;

public class SentryMauiLogcatsTests
{
private class Fixture
{
public MauiAppBuilder Builder { get; }
public FakeTransport Transport { get; private set; } = new FakeTransport();
public InMemoryDiagnosticLogger Logger { get; private set; } = new InMemoryDiagnosticLogger();

public Fixture()
{
var builder = MauiApp.CreateBuilder();
builder.Services.AddSingleton(Substitute.For<IApplication>());

builder.Services.Configure<SentryMauiOptions>(options =>
{
options.Transport = Transport;
options.Dsn = ValidDsn;
options.AttachScreenshot = false; //Disable the screenshot attachment to have the logcat as primary attachment
options.Debug = true;
options.DiagnosticLogger = Logger;
options.AutoSessionTracking = false; //Get rid of session envelope for easier Assert
options.CacheDirectoryPath = null; //Do not wrap our FakeTransport with a caching transport
options.FlushTimeout = TimeSpan.FromSeconds(10);
});
Builder = builder;
}
}

private readonly Fixture _fixture = new();

#if ANDROID
[Fact]
public void CaptureException_CheckLogcatType()
{
var builder = _fixture.Builder.UseSentry(options =>
{
options.Android.LogCatIntegration = Android.LogCatIntegrationType.All;
});

// Arrange
var processor = Substitute.For<ISentryEventProcessorWithHint>();
using var app = builder.Build();
SentryHint hint = null;
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

var scope = new Scope(options);

// Act
processor.Process(Arg.Any<SentryEvent>(), Arg.Do<SentryHint>(h => hint = h)).Returns(new SentryEvent());
options.AddEventProcessor(processor);

_ = new SentryClient(options).CaptureEvent(new SentryEvent(), scope);


// Assert
hint.Should().NotBeNull();
hint.Attachments.First().ContentType.Should().Be("text/plain", hint.Attachments.First().ContentType);
}
#endif
}

0 comments on commit 44d0c69

Please sign in to comment.