forked from ZHPHAN/HaiFengTerminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
78 lines (72 loc) · 2.38 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace hf_terminal
{
static class Program
{
private static string _errLog = string.Empty;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
_errLog = "err_" + Application.ProductName + ".log";
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//你在主线程捕获全部异常就行,如下代码:
//WINFORM未处理异常之捕获
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += Application_ThreadException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
//Application.Run(new FormMain());
Application.Run(new Form1());
}
#region 处理未捕获异常的挂钩函数
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
Exception error = e.Exception;
if (error != null)
{
using (StreamWriter sw = new StreamWriter(_errLog, true))
{
sw.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "\t" + string.Format("出现应用程序未处理的异常 异常类型:{0} 异常消息:{1} 异常位置:{2} ", error.GetType().Name, error.Message, error.StackTrace));
}
}
else
{
using (StreamWriter sw = new StreamWriter(_errLog, true))
{
sw.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "\t" + string.Format("Application ThreadError:{0}", e));
}
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception error = e.ExceptionObject as Exception;
if (error != null)
{
using (StreamWriter sw = new StreamWriter(_errLog, true))
{
sw.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "\t" + string.Format("Application UnhandledException:{0}; 堆栈信息:{1}", error.Message, error.StackTrace));
}
}
else
{
using (StreamWriter sw = new StreamWriter(_errLog, true))
{
sw.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "\t" + string.Format("Application UnhandledError:{0}", e));
}
}
}
#endregion
}
}