using System; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Windows.Forms; using TeamDev.SDK.MVVM; using Thermo.Active.Model; using static Thermo.Active.Model.Constants; using static Thermo.Active.Utils.ThermoActiveLogger; namespace Thermo.Active.Utils { public static class ExceptionManager { public static void ManageException(ERROR_LEVEL errorLevel, Exception exception) { string message = exception.Message; if (exception.InnerException != null) { // Add inner exception message (if exists) message += " | " + exception.InnerException.Message; } // Add method name message += " | " + GetExceptionMethodName(exception); ManageError(errorLevel, message); } private static bool MessageBoxShow = false; public static void ManageError(ERROR_LEVEL errorLevel, string message, bool beforeFormReady = false) { switch (errorLevel) { case ERROR_LEVEL.INFO: { // Log LogInfo(message); // Notify users NotifyUsers(CreateMessageModel("Info!", message, ERROR_LEVEL.INFO)); } break; case ERROR_LEVEL.WARNING: { LogWarning(message); NotifyUsers(CreateMessageModel("Warning!", message, ERROR_LEVEL.WARNING)); } break; case ERROR_LEVEL.ERROR: { LogError(message); NotifyUsersAndClose(CreateMessageModel("Error!", message, ERROR_LEVEL.ERROR)); } break; case ERROR_LEVEL.FATAL: { LogFatal(message); if (!MessageBoxShow) { MessageBoxShow = true; NotifyUsersAndClose(CreateMessageModel("Fatal Error!", message, ERROR_LEVEL.FATAL), beforeFormReady); // Check if the server form is ready (if not i have to close manually) if (beforeFormReady) // Close the application Environment.Exit(1); } } break; default: { LogFatal(message); if (!MessageBoxShow) { MessageBoxShow = true; NotifyUsersAndClose(CreateMessageModel("Generic Error!", message, ERROR_LEVEL.FATAL), beforeFormReady); } } break; } } private static void LogAndNotifyUsers(ErrorMessageModel error) { // Log LogMessage(error.Message, (ERROR_LEVEL)error.ErrorLevel); NotifyUsersAndClose(error); } private static void NotifyUsers(ErrorMessageModel error, bool beforeFormReady = false) { if (beforeFormReady) { MessageBox.Show(new Form { TopMost = true }, error.Message, error.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageServices.Current.Publish(SEND_MESSAGE, null, error); } } private static void NotifyUsersAndClose(ErrorMessageModel error, bool beforeFormReady = false) { if (beforeFormReady) { // Notify user MessageServices.Current.Publish(SEND_STOP_THREADS); MessageBox.Show(new Form { TopMost = true }, error.Message, error.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); MessageServices.Current.Publish(SEND_STOP_SERVER); } else { // Notify user //MessageServices.Current.Publish(SEND_STOP_THREADS); MessageServices.Current.Publish(SEND_MESSAGE, null, error); } } private static ErrorMessageModel CreateMessageModel(string title, string message, ERROR_LEVEL level, string methodName = "") { if (methodName != "") message = message + " | " + methodName; return new ErrorMessageModel() { Title = title, Message = message, ErrorLevel = level }; } public static string GetExceptionMethodName(Exception ex) { var s = new StackTrace(ex, true); var thisasm = Assembly.GetExecutingAssembly(); Console.WriteLine(s.GetFrames().Count()); foreach (var a in s.GetFrames()) { var d = a.GetMethod(); Console.WriteLine(d.Name); } return s.GetFrames().FirstOrDefault()?.GetMethod().Name; } } }