However, system logs may record event messages similar to the following:
Event Type: Warning
Event Source: w3svc
Event Type: None
Event ID: 1009
Date: 9/28/2005
Time: 3: 18: 11
PM User: N/
COMPUTER: IIS-SERVER
Description:
For applications Program The process that the pool defaultapppool provides services unexpectedly terminates. The process ID is "2548 ". Process exited Code It is "0xe0434f4d ".
In addition, application logs may record event messages similar to the following:
Event Type: Error
Event Source:. Net runtime 2.0 Error Report
Event Type: None
Event ID: 5000
Date: 9/28/2005
Time: 3: 18: 02 pm
User: N/
COMPUTER: IIS-SERVER
Description:
Eventtype clr20r3, P1 w3wp.exe, P2 6.0.20.0.1830, P3 1_35be1, P4 app_web_7437ep-9, P5 0.0.0.0, P6 433b1670, P7 9, p8 A, P9 system. exception, P10 nil.
Solution:
Method 1 modify the ihttpmodule object Source code To record the exception information to the application log. The recorded information includes the following:
Abnormal virtual directory path
Exception name
Message
Stack Tracing
To modify the ihttpmodule object, follow these steps.
Note: This code records the message with the event type "error" and event source "ASP. NET 2.0.50727.0" in the application log. To test the module, you can request to use the ASP. NET page of The threadpool. queueuserworkitem method to call the method that causes an unhandled exception.
Put the following code in the file unhandledexceptionmodule. CS. Copy code The Code is as follows: using system;
Using system. diagnostics;
Using system. Globalization;
Using system. IO;
Using system. runtime. interopservices;
Using system. text;
Using system. Threading;
Using system. Web;
Namespace webmonitor {
Public class unhandledexceptionmodule: ihttpmodule {
Static int _ unhandledexceptioncount = 0;
Static string _ sourcename = NULL;
Static object _ initlock = new object ();
Static bool _ initialized = false;
Public void Init (httpapplication APP ){
// Do this one time for each appdomain.
If (! _ Initialized ){
Lock (_ initlock ){
If (! _ Initialized ){
String webenginepath = path. Combine (runtimeenvironment. getruntimedirectory (), "webengine. dll ");
If (! File. exists (webenginepath )){
Throw new exception (string. Format (cultureinfo. invariantculture,
"Failed to locate webengine. dll at '{0}'. This module requires. NET Framework 2.0 .",
Webenginepath ));
}
Fileversioninfo ver = fileversioninfo. getversioninfo (webenginepath );
_ Sourcename = string. Format (cultureinfo. invariantculture, "ASP. NET {0}. {1}. {2}. 0 ",
Ver. filemajorpart, Ver. fileminorpart, Ver. filebuildpart );
If (! EventLog. sourceexists (_ sourcename )){
Throw new exception (string. Format (cultureinfo. invariantculture,
"There is no EventLog source named '{0}'. This module requires. NET Framework 2.0 .",
_ Sourcename ));
}
Appdomain. currentdomain. unhandledexception + = new unhandledexceptioneventhandler (onunhandledexception );
_ Initialized = true;
}
}
}
}
Public void dispose (){
}
Void onunhandledexception (Object o, unhandledexceptioneventargs e ){
// Let this occur one time for each appdomain.
If (interlocked. Exchange (ref _ unhandledexceptioncount, 1 )! = 0)
Return;
Stringbuilder message = new stringbuilder ("\ r \ n \ r \ nunhandledexception logged by unhandledexceptionmodule. dll: \ r \ n \ r \ nappid = ");
String appid = (string) appdomain. currentdomain. getdata (". appid ");
If (appid! = NULL ){
Message. append (appid );
}
exception currentexception = NULL;
for (currentexception = (exception) E. exceptionobject; currentexception! = NULL; currentexception = currentexception. innerexception) {
message. appendformat ("\ r \ n \ r \ ntype = {0} \ r \ n \ r \ nmessage = {1} \ r \ n \ r \ nstack = \ r \ n {2} \ r \ n ",
currentexception. getType (). fullname,
currentexception. message,
currentexception. stacktrace);
}< br> EventLog log = new EventLog ();
log. source = _ sourcename;
log. writeentry (message. tostring (), eventlogentrytype. error);
}< BR >}
unhandledexceptionmodule. save the CS file to the following folder:
C: \ Program Files \ Microsoft Visual Studio 8 \ Vc
open the Microsoft Visual Studio 2005 Command Prompt window.
type sn.exe-K key. SNK and press Enter.
type CSC/T: Library/R: system. Web. dll, system. dll/Keyfile: Key. SNK unhandledexceptionmodule. CS, and then press Enter.
type gacutil.exe/If unhandledexceptionmodule. dll and press Enter.
type ngen install unhandledexceptionmodule. dll and press Enter.
type gacutil/L unhandledexceptionmodule and press enter to display the strong name of the unhandledexceptionmodule file.
9. Add the following code to the Web. config file of an ASP. NET-based application.
type = "webmonitor. unhandledexceptionmodule, "/>
Method 2: Change the unprocessed exception policy back to the default behavior in. NET Framework 1.1 and. NET Framework 1.0.
note: we do not recommend that you change the default behavior. If an exception is ignored, the application may leak the resource and discard the lock.
to enable this default behavior, add the following code to the ASPNET. in the config file: copy the Code the code is as follows: % Windir % \ Microsoft. net \ framework \ v2.0.50727