AppDomain.CurrentDomain.UnhandledException is not raised

I have a WCF service that has the following code in Global.asax:

protected void Application_Start(object sender, EventArgs e) { // Make sure that any exceptions that we don't handle at least get logged. AppDomain.CurrentDomain.UnhandledException += LogUnhandledException; } private void LogUnhandledException(object sender, UnhandledExceptionEventArgs e) { Log.Error.LogException("UnhandledException", e.ExceptionObject as Exception); } 

The idea is to at least log all unused exceptions.

But he seems to never be called. I tried to make Divide by Zero in one of my office operations, and it just stops the service after it gets into the exception.

 int zero = 0; int result = 100 / zero; 

The LogUnhandledException method is never called.

I tried this in both IIS and worked in a debugger.

How can I make this event work for the WCF service?

+6
source share
3 answers

the unhandled exception filter for the application domain is the last attempt to allow the application to register meaningful information before it ceases.

This event provides a notification of uncaught exceptions. It allows the application to log exception information before the system handler reports the exception to the user by default and terminates the application .

If the WCF allowed the exception thrown by the service to be completely non-addressed in this way, this would mean that when the service is hosted in IIS, the entire workflow would be terminated because one request threw an exception - not the desired result. As a result, WCF does not leave exceptions caused by unprocessed services - this event will not be raised in this case.

If you want to log exceptions thrown by WCF services, consider IErrorHandler .

+8
source

Try also catching a ThreadEx exception, for example.

  Application.ThreadException += Application_ThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 

I had the same problem in a WinForms application a while ago. An unhandledException is thrown for truly unhandled exceptions that usually abort your entire process immediately. Typically, there is a global handler that does not allow this, and provides some default behavior. So you need to catch the exception before it goes to this handler. This can be done through a ThreadException.

+2
source

Have you tried this?
From http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(LogUnhandledException); 

I wonder what the difference is between your call and this, with "+ = new" ... but it's worth a try.

0
source

Source: https://habr.com/ru/post/944167/


All Articles