Files
client/App.xaml.cs
2026-05-23 18:16:50 +08:00

38 lines
1.1 KiB
C#

using System.Windows.Threading;
using CloudPrint.Client.Models;
using CloudPrint.Client.Services;
namespace CloudPrint.Client;
public partial class App : System.Windows.Application
{
private readonly FileAppLogger _logger = new();
protected override void OnStartup(System.Windows.StartupEventArgs e)
{
base.OnStartup(e);
DispatcherUnhandledException += OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
}
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
_logger.Write(AppLogEntry.FromException("UI 未处理异常", e.Exception));
e.Handled = true;
}
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception exception)
{
_logger.Write(AppLogEntry.FromException("应用未处理异常", exception));
}
}
private void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
_logger.Write(AppLogEntry.FromException("后台任务未观察异常", e.Exception));
e.SetObserved();
}
}