Skip to content

Commit 44d0c69

Browse files
authored
Feat: Allow logcat attachments to be previewed in Sentry (#3711)
1 parent 0b13b7a commit 44d0c69

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- Android - allow logcat attachments to be previewed in Sentry ([#3711](https://github.com/getsentry/sentry-dotnet/pull/3711))
78
- 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))
89
- Make `Sentry.AspNetCore.Blazor.WebAssembly` generally available. ([#3674](https://github.com/getsentry/sentry-dotnet/pull/3674))
910

src/Sentry/Platforms/Android/LogCatAttachmentEventProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public SentryEvent Process(SentryEvent @event, SentryHint hint)
8181
output.Seek(0, SeekOrigin.Begin);
8282
var bytes = output.ToArray();
8383

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

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.Extensions.Options;
2+
#if ANDROID
3+
using Sentry.Android;
4+
#endif
5+
6+
namespace Sentry.Maui.Tests;
7+
8+
public class SentryMauiLogcatsTests
9+
{
10+
private class Fixture
11+
{
12+
public MauiAppBuilder Builder { get; }
13+
public FakeTransport Transport { get; private set; } = new FakeTransport();
14+
public InMemoryDiagnosticLogger Logger { get; private set; } = new InMemoryDiagnosticLogger();
15+
16+
public Fixture()
17+
{
18+
var builder = MauiApp.CreateBuilder();
19+
builder.Services.AddSingleton(Substitute.For<IApplication>());
20+
21+
builder.Services.Configure<SentryMauiOptions>(options =>
22+
{
23+
options.Transport = Transport;
24+
options.Dsn = ValidDsn;
25+
options.AttachScreenshot = false; //Disable the screenshot attachment to have the logcat as primary attachment
26+
options.Debug = true;
27+
options.DiagnosticLogger = Logger;
28+
options.AutoSessionTracking = false; //Get rid of session envelope for easier Assert
29+
options.CacheDirectoryPath = null; //Do not wrap our FakeTransport with a caching transport
30+
options.FlushTimeout = TimeSpan.FromSeconds(10);
31+
});
32+
Builder = builder;
33+
}
34+
}
35+
36+
private readonly Fixture _fixture = new();
37+
38+
#if ANDROID
39+
[Fact]
40+
public void CaptureException_CheckLogcatType()
41+
{
42+
var builder = _fixture.Builder.UseSentry(options =>
43+
{
44+
options.Android.LogCatIntegration = Android.LogCatIntegrationType.All;
45+
});
46+
47+
// Arrange
48+
var processor = Substitute.For<ISentryEventProcessorWithHint>();
49+
using var app = builder.Build();
50+
SentryHint hint = null;
51+
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
52+
53+
var scope = new Scope(options);
54+
55+
// Act
56+
processor.Process(Arg.Any<SentryEvent>(), Arg.Do<SentryHint>(h => hint = h)).Returns(new SentryEvent());
57+
options.AddEventProcessor(processor);
58+
59+
_ = new SentryClient(options).CaptureEvent(new SentryEvent(), scope);
60+
61+
62+
// Assert
63+
hint.Should().NotBeNull();
64+
hint.Attachments.First().ContentType.Should().Be("text/plain", hint.Attachments.First().ContentType);
65+
}
66+
#endif
67+
}

0 commit comments

Comments
 (0)