using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Text;
namespace WebApi
{
internal static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
// 设置全局异常处理
SetupGlobalExceptionHandling();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainView());
}
///
/// 设置全局异常处理
///
private static void SetupGlobalExceptionHandling()
{
// 设置异常处理模式,让程序继续运行而不是崩溃
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// 处理UI线程异常
Application.ThreadException += Application_ThreadException;
// 处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
///
/// 处理UI线程异常
///
///
///
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
try
{
string errorMessage = GetExceptionDetails(e.Exception);
// 记录异常日志
LogException(errorMessage, "UI线程异常");
// 显示友好的错误提示
MessageBox.Show(
$"程序遇到错误,但已自动处理,程序将继续运行。\n\n错误信息:{e.Exception.Message}\n\n详细信息已记录到日志文件中。",
"程序错误",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
}
catch (Exception ex)
{
// 如果异常处理本身出错,至少尝试显示基本信息
MessageBox.Show($"程序遇到严重错误:{ex.Message}", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// 处理非UI线程异常
///
///
///
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception exception = e.ExceptionObject as Exception;
if (exception != null)
{
string errorMessage = GetExceptionDetails(exception);
// 记录异常日志
LogException(errorMessage, "非UI线程异常");
// 如果是终止性异常,显示更严重的提示
if (e.IsTerminating)
{
MessageBox.Show(
$"程序遇到严重错误,可能需要重启。\n\n错误信息:{exception.Message}\n\n详细信息已记录到日志文件中。",
"严重错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
}
catch (Exception ex)
{
// 最后的保险措施
MessageBox.Show($"程序遇到无法处理的错误:{ex.Message}", "致命错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// 获取异常详细信息
///
///
///
private static string GetExceptionDetails(Exception exception)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"异常时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
sb.AppendLine($"异常类型:{exception.GetType().Name}");
sb.AppendLine($"异常消息:{exception.Message}");
sb.AppendLine($"异常堆栈:{exception.StackTrace}");
if (exception.InnerException != null)
{
sb.AppendLine("内部异常:");
sb.AppendLine(GetExceptionDetails(exception.InnerException));
}
return sb.ToString();
}
///
/// 记录异常日志
///
///
///
private static void LogException(string errorMessage, string exceptionType)
{
try
{
string logDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
string logFile = Path.Combine(logDir, $"Exception_{DateTime.Now:yyyy-MM-dd}.log");
StringBuilder logContent = new StringBuilder();
logContent.AppendLine($"==================== {exceptionType} ====================");
logContent.AppendLine(errorMessage);
logContent.AppendLine(new string('=', 50));
logContent.AppendLine();
File.AppendAllText(logFile, logContent.ToString(), Encoding.UTF8);
}
catch
{
// 如果日志记录失败,也不要抛出异常
}
}
}
}