using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using static SteamWare.Logger.Logging; using static SteamWare.Logger.Constants; using Iob.Model; namespace Iob.Core { public static class ExceptionManager { #region Private Fields private static bool MessageBoxShow = false; #endregion Private Fields #region Public Methods 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; } 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; } } 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); } #endregion Public Methods #region Private Methods 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 }; } 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 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); } #endif } private static void NotifyUsersAndClose(ErrorMessageModel error, bool beforeFormReady = false) { #if 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); } #endif } #endregion Private Methods } }