Unhandled exceptions in. NET Framework 2.0 cause asp.net-based applications to quit unexpectedly _ practical tips

Source: Internet
Author: User
Tags stack trace system log
However, event messages similar to the following may be logged in the System log:
Event Type: Warning
Event Source: W3SVC
Event Category: None
Event id:1009
Date: 9/28/2005
Time: 3:18:11
PM users: N/A
Computer: Iis-server
Describe:
The process providing service for application pool "DefaultAppPool" terminated unexpectedly. The process ID is "2548". The process exit code is "0XE0434F4D".
Also, event messages similar to the following may be logged in the Application log:
Event Type: Error
Event Source:. NET Runtime 2.0 Error Reporting
Event Category: None
Event id:5000
Date: 9/28/2005
Time: 3:18:02 PM
Users: N/A
Computer: Iis-server
Describe:
EventType Clr20r3, P1 w3wp.exe, P2 6.0.3790.1830, P3 42435be1, P4 app_web_7437ep-9, P5 0.0.0.0, P6 433b1670, P7 9, P8 A, P 9 System.Exception, P10 NIL.


Solution:

Method 1 modifies the source code of the IHttpModule object so that the exception information is logged in the Application log. The information that is logged will contain the following:
The virtual directory path where the exception occurred
Exception name
News
Stack trace
To modify the IHttpModule object, follow these steps.
Note: This code will record a message in the application log with the event type "error" and the source of the event as "ASP.net 2.0.50727.0". To test a module, you can request a ASP.net page that uses the ThreadPool.QueueUserWorkItem method to invoke the method that throws an unhandled exception.
Place the following code in a file named UnhandledExceptionModule.cs.
Copy Code code 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 is a 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 the. 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 the. NET Framework 2.0. ",
_sourcename));
}
AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (onunhandledexception);
_initialized = true;
}
}
}
}
public void Dispose () {
}
void OnUnhandledException (Object o, UnhandledExceptionEventArgs e) {
Let I 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\r\n",
Currentexception.gettype (). FullName,
Currentexception.message,
Currentexception.stacktrace);
}
EventLog Log = new EventLog ();
Log.source = _sourcename;
Log.writeentry (message. ToString (), EventLogEntryType.Error);
}
}
}


Save the UnhandledExceptionModule.cs file in 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 then 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 then press Enter.
Type NGen install UnhandledExceptionModule.dll, and then press Enter.
Type gacutil/l unhandledexceptionmodule, and then press Enter to display the strong name of the Unhandledexceptionmodule file.
9. Add the following code to the ASP.net-based application's Web.config file.
<add name= "Unhandledexceptionmodule"
Type= "Webmonitor.unhandledexceptionmodule, <strong name>"/>

Method 2 Changes the unhandled exception policy back to the default behavior that occurs in the. NET Framework 1.1 and. NET Framework 1.0.
Note: We do not recommend that you change the default behavior. If exceptions are ignored, the application may leak resources and discard locks.
To enable this default behavior, add the following code to the Aspnet.config file in the following folder:
Copy Code code as follows:

%windir%\microsoft.net\framework\v2.0.50727
<configuration> br><runtime>
<legacyunhandledexceptionpolicy enabled= "true"/>
</runtime>
</config Uration>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.