調用示例:
// 示例:Program.cs
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args = null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SciTools.RunningExceptionTool.Run(call); // 捕獲運行異常
//SciTools.RunningExceptionTool.Run(call, SendErrors); // 捕獲運行異常,併發送異常信息
//SciTools.RunningExceptionTool.Run(call, SendErrors, args); // 捕獲運行異常,併發送異常信息, Main方法有參數
}
/// <summary>
/// 應用程序,入口邏輯
/// </summary>
public static void call(string[] args = null)
{
Application.Run(new Form1());
}
/// <summary>
// 自定義異常信息處理邏輯
/// </summary>
public static void SendErrors(string errorMsg)
{
// 自定義異常信息處理邏輯
}
}
異常捕獲類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SciTools
{
//// 示例:Program.cs
//static class Program
//{
// /// <summary>
// /// 應用程序的主入口點。
// /// </summary>
// [STAThread]
// static void Main(string[] args = null)
// {
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// SciTools.RunningExceptionTool.Run(call); // 捕獲運行異常
// //SciTools.RunningExceptionTool.Run(call, SendErrors); // 捕獲運行異常,併發送異常信息
// //SciTools.RunningExceptionTool.Run(call, SendErrors, args); // 捕獲運行異常,併發送異常信息, Main方法有參數
// }
// /// <summary>
// /// 應用程序,入口邏輯
// /// </summary>
// public static void call(string[] args = null)
// {
// Application.Run(new Form1());
// }
// /// <summary>
// // 自定義異常信息處理邏輯
// /// </summary>
// public static void SendErrors(string errorMsg)
// {
// // 自定義異常信息處理邏輯
// }
//}
/// <summary>
/// 捕獲運行時異常信息
/// </summary>
public class RunningExceptionTool
{
/// <summary>
/// 定義委託接口處理函數,調用此類中的Main函數爲應用添加異常信息捕獲
/// </summary>
public delegate void MainFunction(string[] args = null);
/// <summary>
/// 異常信息回調處理邏輯
/// </summary>
public delegate void ExceptionMsg(string msg);
private static ExceptionMsg Excall = null;
public static void Run(MainFunction main, ExceptionMsg ExCall = null, string[] args = null)
{
try
{
if (Excall != null) RunningExceptionTool.Excall = ExCall;
// 捕獲
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(CatchThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CatchUnhandledException);
if (main != null)
{
if (args == null) main();
else main(args);
}
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex, string.Empty);
MessageBox.Show(str, "運行異常", MessageBoxButtons.OK, MessageBoxIcon.Error);
//bool ok = (MessageBox.Show(str, "運行異常,提交bug信息?", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK);
//if (ok) sendBugToAuthor(str);
if (Excall != null) Excall(str);
}
}
/// <summary>
/// 捕獲線程異常
/// </summary>
static void CatchThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
MessageBox.Show(str, "運行異常", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (Excall != null) Excall(str);
}
/// <summary>
/// 捕獲未處理異常
/// </summary>
static void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
MessageBox.Show(str, "運行異常", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (Excall != null) Excall(str);
}
/// <summary>
/// 生成自定義異常消息
/// </summary>
/// <param name="ex">異常對象</param>
/// <param name="backStr">備用異常消息:當ex爲null時有效</param>
/// <returns>異常字符串文本</returns>
static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************異常信息****************************");
sb.AppendLine("【出現時間】:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
if (ex != null)
{
sb.AppendLine("【異常類型】:" + ex.GetType().Name);
sb.AppendLine("【異常信息】:" + ex.Message);
sb.AppendLine("【堆棧調用】:" + ex.StackTrace);
sb.AppendLine("【異常方法】:" + ex.TargetSite);
}
else
{
sb.AppendLine("【未處理異常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}
}