123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace HttpServer
- {
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- // 设置未处理异常的处理程序
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
- // 设置应用程序捕获未处理的异常
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- LogException(e.ExceptionObject as Exception);
- }
- private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
- {
- // LogException(e.Exception);
- // 在生产环境中,可以选择立即抛出异常以终止程序,或者在调试时使用以下代码
- e.SetObserved();
- }
- private static void LogException(Exception ex)
- {
- string errorLog = $"Unhandled Exception occurred: {ex.Message}, Stack Trace: {ex.StackTrace}";
- try
- {
- //File.AppendAllText("error.log", errorLog);
- }
- catch
- {
- // 如果写入日志失败,可以考虑其他的错误处理方式
- }
- }
- }
- }
|