| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Core.Mes.Client.Comm.Tool
- {
- public class ExceptionHelper
- {
- private static int _registCnt = 0;
- /// <summary>
- /// 注册未捕获的异常。如果出现未捕获的异常,则自动捕获并写入到本地日志文件中。
- /// </summary>
- public static void RegistException()
- {
- if (_registCnt > 0) return;
- Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- _registCnt++;
- }
- private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
- {
- LogHelper.WriteLogs("Application_ThreadException", GetExceptionMsg(e.Exception));
- }
- private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- LogHelper.WriteLogs("CurrentDomain_UnhandledException", GetExceptionMsg(e.ExceptionObject as Exception));
- }
- private static string GetExceptionMsg(Exception e)
- {
- if (e == null) return "";
- string msg = "错误消息:" + e.Message;
- msg += "\r\n错误源:" + e.Source;
- msg += "\r\n详细信息:" + e.StackTrace;
- return msg;
- }
- }
- }
|