1

I want to be able to log first chance exceptions in an ASP.NET app, and the way you do this is by handling AppDomain.CurrentDomain.FirstChanceException:

    AppDomain.CurrentDomain.FirstChanceException += 
        (object source, FirstChanceExceptionEventArgs e) =>
        {
            Console.WriteLine("FirstChanceException event raised in {0}: {1}",
                AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
        };

But the thing I'm not sure about is when you should call this in an ASP.NET app. Is there one AppDomain per Application, and so this could go in the Application_Start event? Or could there be multiple AppDomains and this would miss some of them? Or could this somehow add the handler twice?

In other words, where should this event handler creation code go?

Joshua Frank
  • 13,120
  • 11
  • 46
  • 95
  • did you tried in ***Global.asax*** ? any sample about it without issues performance ? I have a **dump production** for my application _ASP.NET 4.6.1_. There are _7000 exceptions_ (***first chance exceptions***) in only 20 minutes. – Kiquenet Mar 02 '16 at 11:27
  • http://stackoverflow.com/questions/10697100/appdomain-firstchanceexception-and-stack-overflow-exception for issues ***outofmemory*** and ***stackoverflow*** `exceptions`. _For only debugging purposes_ using `AppDomain.CurrentDomain.FirstChanceException` ? – Kiquenet Mar 02 '16 at 11:48

1 Answers1

0

Application_Start in the Global class should be a good place to place it. However when everything you are trying to do is to capture exceptions, why not use the Application_Error function in the Global.asax file: http://msdn.microsoft.com/en-us/library/24395wz3.aspx

John Saunders
  • 160,644
  • 26
  • 247
  • 397
RononDex
  • 4,143
  • 22
  • 39
  • `Application_Error` will not capture first-chance exceptions. – John Saunders Dec 03 '13 at 19:25
  • Thanks. I will try that. I need this because I've inherited this code and it contains a huge number of Try..Catch blocks that just eat the error and continue, so the exceptions are being "handled", and `Application_Error` doesn't fire. – Joshua Frank Dec 03 '13 at 19:26
  • @JohnSaunders: I think he means why bother with first chance exceptions instead of getting unhandled exceptions via `Application_Error`, but my other comment explains why I need this. – Joshua Frank Dec 03 '13 at 19:27
  • @JohnSaunders: I could, but there are 950 of them, and I'm not sure that the existing functionality doesn't have fatal errors that have been there all along but didn't break the app because of the catch and ignore block. My end goal is to remove them, but I'm afraid that doing that will introduce hundreds of errors, or, more precisely, allow them to break the app when before they were being ignored. This is just a transitional step to log exceptions as I study the problem. – Joshua Frank Dec 03 '13 at 19:32
  • @JohnSaunders Any issues performance in _ASP.NET 4.6.1_? I have a **dump production** for my app.There are _7000_ ***first chance exceptions*** in only 20minutes. I need log _first chance exceptions_ safely for study the problem, without get ***stackoverflow exceptions***. Anyways, ***http://stackoverflow.com/questions/10697100/appdomain-firstchanceexception-and-stack-overflow-exception*** for issues ***outofmemory*** and ***stackoverflow*** `exceptions`. _For only debugging purposes_ using `AppDomain.CurrentDomain.FirstChanceException` in **Application_Start** in Global.asax? – Kiquenet Mar 02 '16 at 11:49
  • First chance exceptions should be ignored. Most of them will be caught. – John Saunders Mar 02 '16 at 12:05