forked from SweetSmellFox/MFAWPF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
105 lines (94 loc) · 3.42 KB
/
App.xaml.cs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using MFAWPF.Utils;
using HandyControl.Controls;
using HandyControl.Tools;
using WPFLocalizeExtension.Engine;
namespace MFAWPF;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
this.Startup += new StartupEventHandler(App_Startup);
this.Exit += new ExitEventHandler(App_Exit);
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
}
void App_Startup(object sender, StartupEventArgs e)
{
//UI线程未捕获异常处理事件
this.DispatcherUnhandledException +=
new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
//非UI线程未捕获异常处理事件
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void App_Exit(object sender, ExitEventArgs e)
{
//程序退出时需要处理的业务
}
void App_DispatcherUnhandledException(object sender,
DispatcherUnhandledExceptionEventArgs e)
{
try
{
e.Handled = true; //把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出
Console.WriteLine(e.Exception);
}
catch (Exception ex)
{
//此时程序出现严重异常,将强制结束退出
LoggerService.LogError(ex.ToString());
Growls.Error("UI线程发生致命错误!");
}
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
StringBuilder sbEx = new StringBuilder();
if (e.IsTerminating)
{
sbEx.Append("非UI线程发生致命错误");
}
sbEx.Append("非UI线程异常:");
if (e.ExceptionObject is Exception ex)
{
Console.WriteLine(ex);
LoggerService.LogError(ex.ToString());
sbEx.Append((ex).Message);
}
else
{
LoggerService.LogError(e.ExceptionObject);
sbEx.Append(e.ExceptionObject);
}
Growls.Error(sbEx.ToString());
}
void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
//task线程内未处理捕获
Growls.Error("Task线程异常:" + e.Exception.Message);
LoggerService.LogError(e.Exception);
foreach (Exception item in e.Exception.InnerExceptions)
{
Console.WriteLine("异常类型:{0}{1}来自:{2}{3}异常内容:{4}",
item.GetType(), Environment.NewLine, item.Source,
Environment.NewLine, item.Message);
LoggerService.LogError(string.Format("异常类型:{0}{1}来自:{2}{3}异常内容:{4}",
item.GetType(), Environment.NewLine, item.Source,
Environment.NewLine, item.Message));
}
e.SetObserved(); //设置该异常已察觉(这样处理后就不会引起程序崩溃)
}
}