winform全局捕获异常
2019-06-29 09:23
10 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36248777/article/details/94123575
[code] public static void ExceptionControl() { //设置应用程序处理异常方式:ThreadException处理 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += (sender, e) => { var str = GetExceptionMsg(e.Exception, e.ToString()); var messageForm =new ErrorMessageForm(); messageForm.SetMessage(str); var flg = FormIsShow(messageForm.Name); if (flg) return; messageForm.ShowDialog(); }; //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += (sender, e)=> { var str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); var messageForm = new ErrorMessageForm(); messageForm.SetMessage(str); var flg = FormIsShow(messageForm.Name); if (flg) return; messageForm.ShowDialog(); }; } private static string GetExceptionMsg(Exception ex, string backStr) { var sb = new StringBuilder(); if (ex == null) { sb.AppendLine("errmsg:" + backStr); } else { if (ex.GetType().Name.ToLower() == "exception") { return ex.Message; } sb.AppendLine("msgtime:" + DateTime.Now); sb.AppendLine("msgtype:" + ex.GetType().Name); sb.AppendLine("callstack:" + ex.Message); sb.AppendLine("untreated:" + ex.StackTrace); } return sb.ToString(); }
调用
[code]FormTools.ExceptionControl();
相关文章推荐
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
- Winform捕获全局异常
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
- C# WinForm捕获全局异常
- winform 记录全局异常捕获
- 在C#代码中应用Log4Net在Winform、Web和WPF中捕获全局异常
- C# WINFORM 捕获全局异常
- C# WinForm捕获全局异常
- C# WinForm捕获全局异常
- C# WinForm捕获全局异常
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
- C# winform 捕获全局异常
- C# WINFORM 捕获全局异常
- C# winform 捕获全局异常
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
- C# winform 捕获全局异常
- 转:如何捕获winform程序全局异常?
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
- winform 记录全局异常捕获
- winform程序不能同时开启两个应用程序的判断以及捕获全局未处理异常