The w3wp process crashes, and an error is left in the system log, but few error messages are left. How can we quickly find out the cause of w3wp crash?
First, write a row.CodeCrash w3wp:
View Source
Print?
1 |
Protected Void Page_load ( Object Sender, eventargs E) |
3 |
Threadpool. queueuserworkitem ( Delegate ( Object Nouse ){ Throw New Exception ( "Go to hell w3wp! " );}); |
This line of code will cause the w3wp process to crash, because after Asp.net 2.0, the default method for Microsoft to handle multithreaded operation exceptions has changed. If the thread, threadpool, or system. threading. if an unhandled exception occurs in the timer execution method, the current execution process stops and crashes.
Of course, in actual situations, we will not intentionally write such a line of code to cause w3wp to crash. Maybe you will say that I generally do notProgramEven if thread or threadpool is used, a crash may occur, because thread or system is also used in system methods such as database connection pool and httpruntime management request queue. threading. timer class, or threadpool. I have personally encountered a system crash because the SQL Server database connection pool is exhausted and a new database connection is unavailable, this is the same principle as the crash caused by the above code line.
It can be said that this crash is a situation that Microsoft designed to crash. Microsoft believes that the process should crash if an exception occurs during multi-thread execution. However, this kind of crash often leaves few system logs. We can only see the crash errors, but not the specific points of the crash or the call stack of the crash. As the saying goes: "There is always more way than suffering". Although the system has no records, we can use code to record the cause of the crash. We have two ways to get the cause of the crash.
The first method is to use reflection to record the exit stack and reason of httpruntime in the global application_end event. If you have a decompilation tool, you can decompile the system. Web. httpruntime class. You can see that this class has two private fields:
View Source
Print?
1 |
Private String _ Shutdownmessage; |
2 |
Private String _ Shutdownstack; |
They record the Exit message and exit stack of the w3wp process. We usually record the values of these two variables in the application_end event to see the collapsed stack. See the code for details.
View Source
Print?
01 |
Protected Void Application_end ( Object Sender, eventargs E) |
03 |
Httpruntime runtime = (httpruntime) Typeof (System. Web. httpruntime). invokemember ( "_ Theruntime" , |
04 |
Bindingflags. nonpublic | bindingflags. Static | bindingflags. getfield, |
12 |
String Shutdownmessage = ( String ) Runtime. GetType (). invokemember ( "_ Shutdownmessage" , |
13 |
Bindingflags. nonpublic | bindingflags. instance | bindingflags. getfield, |
18 |
String Shutdownstack = ( String ) Runtime. GetType (). invokemember ( |
20 |
Bindingflags. nonpublic | bindingflags. instance | bindingflags. getfield, |
25 |
If (! EventLog. sourceexists ( ". Netruntime" )) |
28 |
EventLog. createeventsource ( ". Netruntime" , "Application" ); |
32 |
EventLog = New EventLog (); |
34 |
Log. Source = ". Net RunTime" ; |
36 |
Log. writeentry (string. Format ( "\ R \ n \ r \ n_shutdownmessage = {0} \ r \ n \ r \ n_shutdownstack = {1 }" , Shutdownmessage, shutdownstack), eventlogentrytype. Error ); |
The above code mainly uses reflection, which is not described in detail in this article.
The above method usually gets the call stack of w3wp crash, and we have another method to get it. The appdomain. currentdomain. unhandledexception event is used to record the cause of the unhandled exception. The Code is also simple:
View Source
Print?
01 |
Protected Void Application_start ( Object Sender, eventargs E) |
03 |
Appdomain. currentdomain. unhandledexception + = New Unhandledexceptioneventhandler (currentdomain_unhandledexception ); |
06 |
Void Currentdomain_unhandledexception ( Object Sender, unhandledexceptioneventargs E) |
08 |
Exception EX = E. exceptionobject As Exception; |
09 |
String MSG = ex. message; |
10 |
String Stack = ex. stacktrace; |
The above Code adds a processing method to the appdomain's unhandleexception time during application start to record the cause of the unprocessed exception.