The Application System of a recent project is always ended for unknown reasons. Because the application is a Windows service, debugging is very troublesome, and the problem persists. When a problem occurs, there is no record in the log, and the process of the application has ended. no useful information is found in the System Event Log and Dr. Watson. Considering that the reproduction of the problem requires a lot of information support, in addition, the program does need to collect more information when exceptions occur, so add the uncaught Exception Processing for the program, this process contains the Dump file generated by code.
The general practice is to add an AppDomain. CurrentDomain. UnhandledException event to trigger the event when an uncaptured exception is detected. Then, the dump file is generated using the windbg API function: MiniDumpWriteDump to analyze the problem.
The following sample code handles the AppDomain. CurrentDomain. UnhandledException event that does not capture exceptions:
Code
// In the initial stage of the program, add the handler to the AppDomain. CurrentDomain. UnhandledException event to trigger the event when no exception is captured.
// Place the statement in the program initialization function.
AppDomain. CurrentDomain. UnhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException );
// This is a processing function, which is in the same class as the initialization function.
Void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e. ExceptionObject as Exception;
// These two are custom logging functions,
// DebugLog. Error ("Unhandled Exception occur in Method:" + ex. TargetSite. ToString ());
// DebugLog. Error (ex. ToString ());
// Generate the dump file
MiniDump. TryDump (@ "D: \ MiniDmp. dmp", MiniDump. MiniDumpType. WithFullMemory );
}
After the definition is completed, all exceptions captured by try... catch in the program will trigger the AppDomain. CurrentDomain. UnhandledException event. After the event processing is complete, the exception will continue to be thrown up, and the program will terminate because of this uncaptured exception. At the same time, the Windows event log will record this exception and then trigger the default debugging engine of the system. Therefore, the response to AppDomain. currentDomain. the UnhandledException event does not mean that your application will not be terminated due to exceptions not captured, AppDomain. currentDomain. the UnhandledException event only enables you to automatically collect more information in this case.
The following describes how to generate a dump file. To generate a dump file through code, you need to use the API function MiniDumpWriteDump in dbghelp. dll of windbg (which can be found in the installation directory of windbg) (for more detailed usage, refer to MSDN ). The principle is not enough. It is nothing more than the import dll and then the call. The Code is as follows:
Code
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;
Using System. Diagnostics;
Using System. IO;
Using System. Threading;
Namespace DumpCreator
{
/**/
/// <Summary>
/// This type should be used in Versions later than windows 5.1. If your windows is old, copy the dll in Windbg, which is generally normal.
/// DbgHelp. dll is a built-in dll file in windows.
/// </Summary>
Public static class MiniDump
{
/**/
/*
* Import DbgHelp. dll
*/
[DllImport ("DbgHelp. dll")]
Private static extern Boolean MiniDumpWriteDump (
IntPtr hProcess,
Int32 processId,
IntPtr fileHandle,
MiniDumpType dumpType,
Ref MinidumpExceptionInfo raise info,
IntPtr userInfo,
IntPtr extInfo );
/**/
/*
* MINIDUMP_EXCEPTION_INFORMATION macro information
*/
Struct MinidumpExceptionInfo
{
Public UInt32 ThreadId;
Public IntPtr ExceptionPointers;
Public UInt32 ClientPointers;
}
[DllImport ("kernel32.dll")]
Private static extern uint GetCurrentThreadId ();
/**/
/*
* A self-packaged Function
*/
Public static Boolean TryDump (String dmpPath, MiniDumpType dmpType)
{
// Use a file stream to create a. dmp File
Using (FileStream stream = new FileStream (dmpPath, FileMode. Create ))
{
// Obtain Process Information
Process process = Process. GetCurrentProcess ();
// MINIDUMP_EXCEPTION_INFORMATION Initialization
MinidumpExceptionInfo mei = new MinidumpExceptionInfo ();
Mei. ThreadId = (UInt32) GetCurrentThreadId ();
Mei. ExceptionPointers = Marshal. GetExceptionPointers ();
Mei. ClientPointers = 1;
// The Win32 API called here
Boolean res = MiniDumpWriteDump (
Process. Handle,
Process. Id,
Stream. SafeFileHandle. DangerousGetHandle (),
DmpType,
Ref mei,
IntPtr. Zero,
IntPtr. Zero );
// Clear stream
Stream. Flush ();
Stream. Close ();
Return res;
}
}
Public enum MiniDumpType
{
None = 0x00010000,
Normal = 0x00000000,
WithDataSegs = 0x00000001,
WithFullMemory = 0x00000002,
WithHandleData = 0x00000004,
FilterMemory = 0x00000008,
ScanMemory = 0x00000010,
WithUnloadedModules = 0x00000020,
WithIndirectlyReferencedMemory = 0x00000040,
FilterModulePaths = 0x00000080,
WithProcessThreadData = 0x00000100,
WithPrivateReadWriteMemory = 0x00000200,
WithoutOptionalData = 0x00000400,
WithFullMemoryInfo = 0x00000800,
WithThreadInfo = 0x00001000,
WithCodeSegs = 0x00002000
}
}
}
This code is shown in "how to get the program to get the dump file, MiniDumpWriteDump windbg extension", and some modifications are made later. The adjustment mainly adds the GetCurrentThreadId () system API function to obtain the system ThreadID of the current thread. After the dump file generated by using the original code is loaded using windbg, the system will prompt that the current thread cannot be found. This problem is estimated to be caused by the author mistakenly passing the managed thread ID as the system thread ID to MiniDumpWriteDump.
After completing these two steps, you can deal with uncaptured exceptions that cause headaches. However, this method can only handle uncaptured exceptions in a hosted environment. If it is a hosted environment, an unmanaged exception does not seem to be captured.
In addition, when the windbg analyzes dump, the top function of the stack of the exception thread is the UnhandledException event function ......
References:
How to let the program catch the dump file, MiniDumpWriteDump windbg Extension
Handling of exceptions not captured in. Net
"