public static bool HandleException(
     Exception exception,
     Form form,
     KeyEventHandler keyEventHandler,
     string eventDetailsMessage,
     bool emailException,
     out string emailErrorMessage,
     out string emailLogMessageText,
     List <EmailNotificationRecipient> emailNotificationRecipients)
 {
     try
     {
         emailErrorMessage   = string.Empty;
         emailLogMessageText = string.Empty;
         bool closeApplication = false;
         if (exception == null)
         {
             throw new NullReferenceException("exception to be handled may not be null.");
         }
         if (GOCWindows.Instance.ShowMessageBoxOnException)
         {
             UIHelperWindows.DisplayException(exception, form, keyEventHandler, eventDetailsMessage);
         }
         if (GOCWindows.Instance.ShowWindowsFormsNotificationOnException && GOCWindows.Instance.NotifyIcon != null)
         {
             string applicationName = string.IsNullOrEmpty(GOCWindows.Instance.ApplicationName) ? "Notification" : GOCWindows.Instance.ApplicationName;
             GOCWindows.Instance.NotifyIcon.ShowBalloonTip(10, applicationName, exception.Message, ToolTipIcon.Error);
         }
         UserThrownException userThrownException = exception as UserThrownException;
         if (userThrownException != null)
         {
             closeApplication = userThrownException.CloseApplication;
             GOCWindows.Instance.Logger.LogMessage(new LogError(exception, eventDetailsMessage, userThrownException.LoggingLevel));
         }
         else
         {
             GOCWindows.Instance.Logger.LogMessage(new LogError(exception, eventDetailsMessage, LoggingLevel.Normal));
         }
         if (emailException && GOCWindows.Instance.SendEmailOnException)
         {
             if (GOCWindows.Instance.EmailClient == null)
             {
                 throw new Exception(string.Format("{0} enabled, but {1} not set.",
                                                   EntityReaderGeneric <GOCWindows> .GetPropertyName(p => p.SendEmailOnException, false),
                                                   EntityReaderGeneric <GOCWindows> .GetPropertyName(p => p.EmailClient, false)));
             }
             GOCWindows.Instance.EmailClient.SendExceptionEmailNotification(exception, out emailErrorMessage, out emailLogMessageText, GOCWindows.Instance.AppendHostNameToExceptionEmails, emailNotificationRecipients);
         }
         return(closeApplication);
     }
     catch (Exception ex)
     {
         Exception wrappedException = new Exception(ex.Message, exception);
         Console.WriteLine(LogError.GetErrorMessageFromException(new Exception(ex.Message, exception), eventDetailsMessage));
         GOCWindows.Instance.Logger.LogMessageToFile(new LogError(wrappedException, eventDetailsMessage, LoggingLevel.Minimum));
         emailErrorMessage   = string.Empty;
         emailLogMessageText = string.Empty;
         return(true);
     }
 }
Beispiel #2
0
 public static bool HandleException(
     Exception exception,
     string eventDetailsMessage,
     bool emailException,
     out string emailErrorMessage,
     out string emailLogMessageText,
     List <EmailNotificationRecipient> emailNotificationRecipients)
 {
     try
     {
         emailErrorMessage   = string.Empty;
         emailLogMessageText = string.Empty;
         bool closeApplication = false;
         if (exception == null)
         {
             throw new NullReferenceException("exception to be handled may not be null.");
         }
         UserThrownException userThrownException = exception as UserThrownException;
         if (userThrownException != null)
         {
             closeApplication = userThrownException.CloseApplication;
             GOC.Instance.Logger.LogMessage(new LogError(exception, eventDetailsMessage, userThrownException.LoggingLevel));
         }
         else
         {
             GOC.Instance.Logger.LogMessage(new LogError(exception, eventDetailsMessage, LoggingLevel.Normal));
         }
         if (emailException && GOC.Instance.SendEmailOnException)
         {
             if (GOC.Instance.EmailClient == null)
             {
                 throw new Exception(string.Format("{0} enabled, but {1} not set.",
                                                   EntityReaderGeneric <GOC> .GetPropertyName(p => p.SendEmailOnException, false),
                                                   EntityReaderGeneric <GOC> .GetPropertyName(p => p.EmailClient, false)));
             }
             GOC.Instance.EmailClient.SendExceptionEmailNotification(exception, out emailErrorMessage, out emailLogMessageText, GOC.Instance.AppendHostNameToExceptionEmails, emailNotificationRecipients);
         }
         return(closeApplication);
     }
     catch (Exception ex)
     {
         Exception wrappedException = new Exception(ex.Message, exception);
         Console.WriteLine(LogError.GetErrorMessageFromException(new Exception(ex.Message, exception), eventDetailsMessage));
         GOC.Instance.Logger.LogMessageToFile(new LogError(wrappedException, eventDetailsMessage, LoggingLevel.Minimum));
         emailErrorMessage   = string.Empty;
         emailLogMessageText = string.Empty;
         return(true);
     }
 }