private static void MiniDumpToFile(string fileToDump, MinidumpTypes dumpType) { using var fsToDump = File.Open(fileToDump, FileMode.Create, FileAccess.ReadWrite, FileShare.Write); using var thisProcess = Process.GetCurrentProcess(); var mINIDUMP_EXCEPTION_INFORMATION = new MINIDUMP_EXCEPTION_INFORMATION { ClientPointers = false, #if WITH_EXCEPTIONPOINTERS ExceptionPointers = Marshal.GetExceptionPointers(), #else ExceptionPointers = IntPtr.Zero, #endif ThreadId = SafeNativeMethods.GetCurrentThreadId(), }; var error = SafeNativeMethods.MiniDumpWriteDump( thisProcess.Handle, thisProcess.Id, fsToDump.SafeFileHandle, dumpType, ref mINIDUMP_EXCEPTION_INFORMATION, IntPtr.Zero, IntPtr.Zero); if (error > 0) { DumpMessage?.Invoke(typeof(MiniDump), new MessageEventArgs($"Mini-dumping failed with Code: {error}", "Error!", ErrorLevel.Error)); } }
internal static void ExceptionEventHandlerCode(Exception e, bool threadException) { var exceptionData = $"{e.GetType()}: {e.Message}{Environment.NewLine}{e.StackTrace}{Environment.NewLine}"; PrintInnerExceptions(e, ref exceptionData); // do not dump or close if in a debugger. if (!Debugger.IsAttached) { ForceClosure.ForceClose = true; // if this is not Windows, MiniDumpToFile(), which calls MiniDumpWriteDump() in dbghelp.dll // cannot be used as it does not exist. I need to figure out mini-dumping for // these platforms manually. In that case I guess it does not matter much anyway // with the world of debugging and running in a IDE. #if NET5_0_OR_GREATER if (OperatingSystem.IsWindows()) #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) #endif { if (string.IsNullOrEmpty(MiniDumpAttribute.CurrentInstance.DumpLogFileName)) { MiniDumpAttribute.CurrentInstance.DumpLogFileName = SettingsFile.ErrorLogPath; } if (string.IsNullOrEmpty(MiniDumpAttribute.CurrentInstance.DumpFileName)) { MiniDumpAttribute.CurrentInstance.DumpFileName = SettingsFile.MiniDumpPath; } File.WriteAllText(MiniDumpAttribute.CurrentInstance.DumpLogFileName, exceptionData); MiniDumpToFile(MiniDumpAttribute.CurrentInstance.DumpFileName, MiniDumpAttribute.CurrentInstance.DumpType); DumpMessage?.Invoke(typeof(MiniDump), new MessageEventArgs(string.Format(CultureInfo.InvariantCulture, MiniDumpAttribute.CurrentInstance.Text, MiniDumpAttribute.CurrentInstance.DumpLogFileName), threadException ? MiniDumpAttribute.CurrentInstance.ThreadExceptionTitle : MiniDumpAttribute.CurrentInstance.ExceptionTitle, ErrorLevel.Error)); } } }
/* * /// <summary> * /// Occurs when a mini-dump fails with any sort of error code. * /// </summary> * public static event EventHandler<MessageEventArgs> DumpFailed; */ internal static void ExceptionEventHandlerCode(Exception e, bool threadException) { var exceptionData = $"{e.GetType()}: {e.Message}{Environment.NewLine}{e.StackTrace}{Environment.NewLine}"; var outputData = Encoding.ASCII.GetBytes(exceptionData); // do not dump or close if in a debugger. if (!Debugger.IsAttached) { ForceClosure.ForceClose = true; // if this is not Windows, MiniDumpToFile(), which calls MiniDumpWriteDump() in dbghelp.dll // cannot be used as it does not exist. I need to figure out mini-dumping for // these platforms manually. In that case I guess it does not matter much anyway // with the world of debugging and running in a IDE. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (string.IsNullOrEmpty(MiniDumpAttribute.CurrentInstance.DumpLogFileName)) { MiniDumpAttribute.CurrentInstance.DumpLogFileName = SettingsFile.ErrorLogPath; } if (string.IsNullOrEmpty(MiniDumpAttribute.CurrentInstance.DumpFileName)) { MiniDumpAttribute.CurrentInstance.DumpFileName = SettingsFile.MiniDumpPath; } using (var fileStream = File.OpenWrite(MiniDumpAttribute.CurrentInstance.DumpLogFileName)) { fileStream.Write(outputData, 0, outputData.Length); } MiniDumpToFile(MiniDumpAttribute.CurrentInstance.DumpFileName, MiniDumpAttribute.CurrentInstance.DumpType); DumpMessage?.Invoke(typeof(MiniDump), new MessageEventArgs(string.Format(MiniDumpAttribute.CurrentInstance.Text, MiniDumpAttribute.CurrentInstance.DumpLogFileName), threadException ? MiniDumpAttribute.CurrentInstance.ThreadExceptionTitle : MiniDumpAttribute.CurrentInstance.ExceptionTitle, ErrorLevel.Error)); } } }