babgvant / elmah

Automatically exported from code.google.com/p/elmah
Apache License 2.0
0 stars 0 forks source link

Unhandled errors that kill w3p process need to be logged #199

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
There are unhanded exceptions, that cause IIS process to die and hence only log 
to Event Log (and these fatal exceptions are caused by code). Currently, they 
are not logged by Elmah. Adding the
following exception will help deal with that. Here is an article about 
unhandled exceptions: http://support.microsoft.com/kb/911816

I have tested this and it works. It will be great addition. I currently have to 
have my own module to write these exceptions, but adding to Elmah will help to 
centralize all the errors.

Step 1: Add this to OnInit()
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledException);

Step 2:Add this function to let Elmah log the error.
void OnUnhandledException(object o, UnhandledExceptionEventArgs e)
       {
           // Let this occur one time for each AppDomain.
           if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
               return;

           //try
           //{
               StringBuilder message = new StringBuilder("\r\n\r\nUnhandledException was caught and logged by ASUnhandledExceptionModule:\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);
               }               

               Elmah.Error err = new Elmah.Error(new Exception(message.ToString()));
               Elmah.ErrorLog.GetDefault(null).Log(err);
}

Original issue reported on code.google.com by hellobh...@gmail.com on 13 Jan 2011 at 2:10

GoogleCodeExporter commented 8 years ago
This looks like a good candidate to add to ELMAH Sandbox[1] since it does not 
seem to require a change of the ELMAH source code. It can live in its own 
module and have its own initialization and options. You can clone the Sandbox 
repository and contribute your idea above.

[1] http://code.google.com/p/elmah-sandbox/

Original comment by azizatif on 13 Jan 2011 at 1:58