/// <summary> /// Handle an uncaught app domain exception by writing a last crash report /// </summary> public static void HandleCrash(object sender, UnhandledExceptionEventArgs e) { // Build crash Text string crashText; try { crashText = ExceptionPrinter.Print((Exception)e.ExceptionObject); } catch { var ex = e.ExceptionObject as Exception; crashText = "Some dumbass actually threw an exception in the exception: \n" + $" Type of trojan exception: {e.ExceptionObject.GetType()}\n" + $" Original stack trace: \n{(ex == null ? "Exception does not inherit System.Exception" : ex.StackTrace)}"; } // First dump to console Console.WriteLine(crashText); // Dump to file WriteErrorToFile(crashText); if (e.IsTerminating && !Debugger.IsAttached) { Console.ReadLine(); Environment.Exit(2); } }
/// <summary> /// Load all assemblies from directory /// </summary> public static void LoadAssemblies() { var parentDir = AppDomain.CurrentDomain.BaseDirectory; var assemblies = Directory.GetFiles(parentDir, "*.dll", SearchOption.TopDirectoryOnly); foreach (var assemblyFile in assemblies) { if (AppDomain.CurrentDomain.GetAssemblies().Any(loadedAssembly => NamesMatch(loadedAssembly, assemblyFile))) { continue; } try { Assembly.LoadFrom(assemblyFile); } catch (Exception ex) { var loadEx = new FileLoadException("Failed to load assembly file " + assemblyFile, ex); Console.WriteLine(ExceptionPrinter.Print(loadEx)); } } }