-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
48 lines (42 loc) · 1.58 KB
/
App.xaml.cs
File metadata and controls
48 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
namespace Usely
{
public partial class App : Application
{
private string _logPath = Path.Combine(AppContext.BaseDirectory, "usely-error.log");
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
DispatcherUnhandledException += App_DispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
base.OnStartup(e);
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
LogException(e.Exception);
MessageBox.Show("Une erreur inattendue est survenue. Voir usely-error.log.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
LogException(ex);
}
private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
LogException(e.Exception);
e.SetObserved();
}
private void LogException(Exception ex)
{
try
{
File.AppendAllText(_logPath, $"[{DateTime.Now}] {ex}\n\n");
}
catch { }
}
}
}