Skip to content

Commit e81f986

Browse files
authored
Code Quality: Added a logger to send exception reports to Sentry (#15618)
1 parent 9a6fee8 commit e81f986

File tree

5 files changed

+67
-20
lines changed

5 files changed

+67
-20
lines changed

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Files.App.Helpers.Application;
66
using Files.App.Services.SizeProvider;
77
using Files.App.Storage.Storables;
8+
using Files.App.Utils.Logger;
89
using Files.App.ViewModels.Settings;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Hosting;
@@ -138,6 +139,7 @@ public static IHost ConfigureHost()
138139
.UseEnvironment(AppLifecycleHelper.AppEnvironment.ToString())
139140
.ConfigureLogging(builder => builder
140141
.AddProvider(new FileLoggerProvider(Path.Combine(ApplicationData.Current.LocalFolder.Path, "debug.log")))
142+
.AddProvider(new SentryLoggerProvider())
141143
.SetMinimumLevel(LogLevel.Information))
142144
.ConfigureServices(services => services
143145
// Settings services
@@ -265,14 +267,6 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
265267
ex.Data[Mechanism.HandledKey] = false;
266268
ex.Data[Mechanism.MechanismKey] = "Application.UnhandledException";
267269

268-
SentrySdk.CaptureException(ex, scope =>
269-
{
270-
scope.User.Id = generalSettingsService.UserId;
271-
});
272-
273-
274-
SentrySdk.CaptureException(ex);
275-
276270
formattedException.AppendLine($">>>> HRESULT: {ex.HResult}");
277271

278272
if (ex.Message is not null)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Microsoft.Extensions.Logging;
5+
using Sentry;
6+
7+
namespace Files.App.Utils.Logger
8+
{
9+
public sealed class SentryLogger : ILogger
10+
{
11+
12+
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
13+
{
14+
return null;
15+
}
16+
17+
public bool IsEnabled(LogLevel logLevel)
18+
{
19+
return true;
20+
}
21+
22+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
23+
{
24+
if (exception is null)
25+
return;
26+
27+
var generalSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
28+
29+
var level = logLevel switch
30+
{
31+
LogLevel.Debug => SentryLevel.Debug,
32+
LogLevel.Information => SentryLevel.Info,
33+
LogLevel.Warning => SentryLevel.Warning,
34+
LogLevel.Error => SentryLevel.Error,
35+
LogLevel.Critical => SentryLevel.Fatal,
36+
_ => SentryLevel.Debug
37+
};
38+
39+
SentrySdk.CaptureException(exception, scope =>
40+
{
41+
scope.User.Id = generalSettingsService?.UserId;
42+
scope.Level = level;
43+
});
44+
}
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Files.App.Utils.Logger
7+
{
8+
public sealed class SentryLoggerProvider : ILoggerProvider
9+
{
10+
public ILogger CreateLogger(string categoryName)
11+
{
12+
return new SentryLogger();
13+
}
14+
15+
public void Dispose()
16+
{
17+
}
18+
}
19+
}

src/Files.App/ViewModels/UserControls/InfoPaneViewModel.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ private async void ContentPageContext_PropertyChanged(object? sender, PropertyCh
153153
// Handle exception in case WinUI Windows is closed
154154
// (see https://github.com/files-community/Files/issues/15599)
155155

156-
SentrySdk.CaptureException(ex, scope =>
157-
{
158-
scope.User.Id = generalSettingsService.UserId;
159-
scope.Level = SentryLevel.Warning;
160-
});
161-
162156
App.Logger.LogWarning(ex, ex.Message);
163157
}
164158

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,6 @@ private void LoadPaneChanged()
462462
// Handle exception in case WinUI Windows is closed
463463
// (see https://github.com/files-community/Files/issues/15599)
464464

465-
SentrySdk.CaptureException(ex, scope =>
466-
{
467-
scope.User.Id = generalSettingsService.UserId;
468-
scope.Level = SentryLevel.Warning;
469-
});
470-
471465
App.Logger.LogWarning(ex, ex.Message);
472466
}
473467
}

0 commit comments

Comments
 (0)