应用程序域未知异常截获并写日志

AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);

void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception exception;
            exception = e.ExceptionObject as Exception;
            if (exception == null)
            {
                if (e.ExceptionObject != null)
                {
                    EventLog log = new EventLog();
                    log.Source = "app";
                    log.WriteEntry(e.ExceptionObject.ToString() + " Terminating:" + e.IsTerminating.ToString(), EventLogEntryType.Error);
                }
            }
            else
            {
                EventLog log = new EventLog();
                log.Source = "app";
                log.WriteEntry("Message:" + exception.Message + " Source:" + exception.Source + " StackTrace:" + exception.StackTrace + " Terminating:" + e.IsTerminating.ToString(), EventLogEntryType.Error);
            }
        }

你可能感兴趣的:(应用程序)