Skip to content

Commit 58c6500

Browse files
Enable logging in MAUI (#1738)
* Enable logging for MAUI * Fix logger filter for sample apps * Update sample app * Update CHANGELOG.md
1 parent 610dee8 commit 58c6500

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Android Scope Sync ([#1737](https://github.com/getsentry/sentry-dotnet/pull/1737))
8+
- Enable logging in MAUI ([#1738](https://github.com/getsentry/sentry-dotnet/pull/1738))
89

910
## Sentry.Maui 3.18.0-preview.1
1011

samples/Sentry.Samples.Maui/MainPage.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
using Microsoft.Extensions.Logging;
2+
13
namespace Sentry.Samples.Maui;
24

35
public partial class MainPage : ContentPage
46
{
7+
private readonly ILogger<MainPage> _logger;
8+
59
int count = 0;
610

7-
public MainPage()
11+
// NOTE: You can only inject an ILogger<T>, not a plain ILogger
12+
public MainPage(ILogger<MainPage> logger)
813
{
14+
_logger = logger;
915
InitializeComponent();
1016
}
1117

@@ -19,6 +25,8 @@ private void OnCounterClicked(object sender, EventArgs e)
1925
CounterBtn.Text = $"Clicked {count} times";
2026

2127
SemanticScreenReader.Announce(CounterBtn.Text);
28+
29+
_logger.LogInformation("The button has been clicked {ClickCount} times", count);
2230
}
2331

2432
private void OnUnhandledExceptionClicked(object sender, EventArgs e)

samples/Sentry.Samples.Maui/MauiProgram.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@ namespace Sentry.Samples.Maui;
22

33
public static class MauiProgram
44
{
5-
public static MauiApp CreateMauiApp() =>
6-
MauiApp.CreateBuilder()
5+
public static MauiApp CreateMauiApp()
6+
{
7+
var builder = MauiApp.CreateBuilder()
78
.UseMauiApp<App>()
9+
10+
// This adds Sentry to your Maui application
811
.UseSentry(options =>
912
{
13+
// The DSN is the only required option.
1014
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
11-
options.Debug = true;
12-
options.MaxBreadcrumbs = 1000; // TODO: reduce breadcrumbs, remove this
15+
16+
// By default, we will send the last 100 breadcrumbs with each event.
17+
// If you want to see everything we can capture from MAUI, you may wish to use a larger value.
18+
options.MaxBreadcrumbs = 1000;
1319
})
20+
1421
.ConfigureFonts(fonts =>
1522
{
1623
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
1724
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18-
})
19-
.Build();
25+
});
26+
27+
// For this sample, we'll also register the main page for DI so we can inject a logger there.
28+
builder.Services.AddTransient<MainPage>();
29+
30+
return builder.Build();
31+
}
2032
}

src/Sentry.Extensions.Logging/SentryLogger.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,7 @@ private bool ShouldCaptureEvent(
131131
Exception? exception)
132132
=> _options.MinimumEventLevel != LogLevel.None
133133
&& logLevel >= _options.MinimumEventLevel
134-
// No events from Sentry code using ILogger
135-
// A type from the main SDK could be used to resolve a logger
136-
// hence 'Sentry' and also 'Sentry.', won't block SentrySomething
137-
// often used by users experimenting with Sentry
138-
&& !CategoryName.StartsWith("Sentry.", StringComparison.Ordinal)
139-
&& !string.Equals(CategoryName, "Sentry", StringComparison.Ordinal)
134+
&& !IsFromSentry()
140135
&& _options.Filters.All(
141136
f => !f.Filter(
142137
CategoryName,
@@ -150,12 +145,29 @@ private bool ShouldAddBreadcrumb(
150145
Exception? exception)
151146
=> _options.MinimumBreadcrumbLevel != LogLevel.None
152147
&& logLevel >= _options.MinimumBreadcrumbLevel
148+
&& !IsFromSentry()
153149
&& _options.Filters.All(
154150
f => !f.Filter(
155151
CategoryName,
156152
logLevel,
157153
eventId,
158-
exception))
159-
&& !CategoryName.StartsWith("Sentry.", StringComparison.Ordinal)
160-
&& !string.Equals(CategoryName, "Sentry", StringComparison.Ordinal);
154+
exception));
155+
156+
157+
private bool IsFromSentry()
158+
{
159+
if (string.Equals(CategoryName, "Sentry", StringComparison.Ordinal))
160+
{
161+
return true;
162+
}
163+
164+
#if DEBUG
165+
if (CategoryName.StartsWith("Sentry.Samples.", StringComparison.Ordinal))
166+
{
167+
return false;
168+
}
169+
#endif
170+
171+
return CategoryName.StartsWith("Sentry.", StringComparison.Ordinal);
172+
}
161173
}

src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.ComponentModel;
22
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Logging;
34
using Microsoft.Extensions.Options;
5+
using Sentry.Extensions.Logging;
46
using Sentry.Extensions.Logging.Extensions.DependencyInjection;
57
using Sentry.Maui;
68
using Sentry.Maui.Internal;
@@ -40,10 +42,6 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn)
4042
public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
4143
Action<SentryMauiOptions>? configureOptions)
4244
{
43-
// TODO: Verify that each of these dependencies is needed.
44-
45-
// builder.Logging.AddConfiguration();
46-
4745
var services = builder.Services;
4846
services.Configure<SentryMauiOptions>(options =>
4947
builder.Configuration.GetSection("Sentry").Bind(options));
@@ -53,6 +51,8 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
5351
services.Configure(configureOptions);
5452
}
5553

54+
services.AddLogging();
55+
services.AddSingleton<ILoggerProvider, SentryLoggerProvider>();
5656
services.AddSingleton<IMauiInitializeService, SentryMauiInitializer>();
5757
services.AddSingleton<IConfigureOptions<SentryMauiOptions>, SentryMauiOptionsSetup>();
5858
services.AddSingleton<Disposer>();

0 commit comments

Comments
 (0)