Files
46-Pingzhi126-ZZ-WebApi/Program.cs
2026-06-08 17:19:25 +08:00

162 lines
5.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// 设置全局异常处理
SetupGlobalExceptionHandling();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainView());
}
/// <summary>
/// 设置全局异常处理
/// </summary>
private static void SetupGlobalExceptionHandling()
{
// 设置异常处理模式,让程序继续运行而不是崩溃
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// 处理UI线程异常
Application.ThreadException += Application_ThreadException;
// 处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
/// <summary>
/// 处理UI线程异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 处理非UI线程异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 获取异常详细信息
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
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();
}
/// <summary>
/// 记录异常日志
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="exceptionType"></param>
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
{
// 如果日志记录失败,也不要抛出异常
}
}
}
}