1 Star 0 Fork 2

冰点xxd / hf_terminal

加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未指定开源许可证,未经作者的许可,此代码仅用于学习,不能用于其他用途。
克隆/下载
Program.cs 2.27 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
海风 提交于 2016-03-30 11:20 . 基础版本
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace HaiFeng
{
static class Program
{
private static string _errLog = string.Empty;
/// <summary>
/// The main entry point for the application.
/// </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 HFForm());
}
#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
}
}

评论 ( 0 )

你可以在登录后,发表评论

1
https://gitee.com/bingdian2001/hf_terminal.git
git@gitee.com:bingdian2001/hf_terminal.git
bingdian2001
hf_terminal
hf_terminal
master

搜索帮助

182229 41614e54 1850385 182230 7885ed45 1850385