Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log to Console and Debug when targeting MAUI #3373

Merged
merged 18 commits into from
Jun 20, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Fixes

- Debug logs are now visible for MAUI apps in Visual Studio when using Sentry's default DiagnosticLogger ([#3373](https://github.com/getsentry/sentry-dotnet/pull/3373))
- Fixed Monitor duration calculation ([#3420]https://github.com/getsentry/sentry-dotnet/pull/3420)
- Fixed null IServiceProvider in anonymous routes with OpenTelemetry ([#3401](https://github.com/getsentry/sentry-dotnet/pull/3401))
- Fixed Trim warnings in Sentry.DiagnosticSource and WinUIUnhandledException integrations ([#3410](https://github.com/getsentry/sentry-dotnet/pull/3410))
Expand Down
7 changes: 7 additions & 0 deletions src/Sentry.Maui/Internal/SentryMauiOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.Maui.Networking;
using Sentry.Infrastructure;

namespace Sentry.Maui.Internal;

Expand Down Expand Up @@ -36,6 +37,12 @@ public void Configure(SentryMauiOptions options)
// Global Mode makes sense for client apps
options.IsGlobalModeEnabled = true;

// So debug logs are visible in both Rider and Visual Studio
if (options is { Debug: true, DiagnosticLogger: null })
{
options.DiagnosticLogger = new ConsoleAndTraceDiagnosticLogger(options.DiagnosticLevel);
}

// We'll use an event processor to set things like SDK name
options.AddEventProcessor(new SentryMauiEventProcessor(options));

Expand Down
22 changes: 22 additions & 0 deletions src/Sentry/Infrastructure/ConsoleAndTraceDiagnosticLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Sentry.Infrastructure;

/// <summary>
/// Logger that logs to both Console and Trace outputs, used in MAUI applications by default, since Visual Studio
/// only show's Trace outputs and Rider shows Console outputs.
/// </summary>
public class ConsoleAndTraceDiagnosticLogger : DiagnosticLogger
{
/// <summary>
/// Creates a new instance of <see cref="ConsoleAndTraceDiagnosticLogger"/>.
/// </summary>
public ConsoleAndTraceDiagnosticLogger(SentryLevel minimalLevel) : base(minimalLevel)
{
}

/// <inheritdoc />
protected override void LogMessage(string message)
{
Console.WriteLine(message);
Trace.WriteLine(message);
}
}
61 changes: 61 additions & 0 deletions test/Sentry.Maui.Tests/SentryMauiAppBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,65 @@ public void UseSentry_WithCaching_CanChangeCacheDirectoryPath()
// Assert
Assert.Equal(cachePath, options.CacheDirectoryPath);
}

[Fact]
public void UseSentry_DebugFalse_LoggerLeftDefault()
{
// Arrange
var builder = _fixture.Builder;

// Act
builder.UseSentry(options =>
{
options.Debug = false;
options.Dsn = ValidDsn;
});

using var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

// Assert
options.DiagnosticLogger.Should().BeNull();
}

[Fact]
public void UseSentry_DebugTrue_ConsoleAndTracingDiagnosticsLogger()
{
// Arrange
var builder = _fixture.Builder;

// Act
builder.UseSentry(options =>
{
options.Debug = true;
options.Dsn = ValidDsn;
});

using var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

// Assert
options.DiagnosticLogger.Should().BeOfType<ConsoleAndTraceDiagnosticLogger>();
}

[Fact]
public void UseSentry_DebugTrue_CustomDiagnosticsLogger()
{
// Arrange
var builder = _fixture.Builder;

// Act
builder.UseSentry(options =>
{
options.Debug = true;
options.Dsn = ValidDsn;
options.DiagnosticLogger = new TraceDiagnosticLogger(SentryLevel.Fatal);
});

using var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

// Assert
options.DiagnosticLogger.Should().BeOfType<TraceDiagnosticLogger>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,11 @@ namespace Sentry.Http
}
namespace Sentry.Infrastructure
{
public class ConsoleAndTraceDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleAndTraceDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
protected override void LogMessage(string message) { }
}
public class ConsoleDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,11 @@ namespace Sentry.Http
}
namespace Sentry.Infrastructure
{
public class ConsoleAndTraceDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleAndTraceDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
protected override void LogMessage(string message) { }
}
public class ConsoleDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,11 @@ namespace Sentry.Http
}
namespace Sentry.Infrastructure
{
public class ConsoleAndTraceDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleAndTraceDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
protected override void LogMessage(string message) { }
}
public class ConsoleDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
Expand Down
5 changes: 5 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,11 @@ namespace Sentry.Http
}
namespace Sentry.Infrastructure
{
public class ConsoleAndTraceDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleAndTraceDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
protected override void LogMessage(string message) { }
}
public class ConsoleDiagnosticLogger : Sentry.Infrastructure.DiagnosticLogger
{
public ConsoleDiagnosticLogger(Sentry.SentryLevel minimalLevel) { }
Expand Down
Loading