Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

c# - AppDomain.CurrentDomain.UnhandledException does not get called

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 exceptions that are unhanded.

But it does not seem to ever be called. I tried doing a Divide by Zero in one of my service operations and it just stops the service after it hits the exception.

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

The LogUnhandledException method never gets called.

I have tried this in both IIS and running in the debugger.

How can I get this event to work for a WCF service?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The unhandled exception filter for an app domain is a last-ditch attempt to allow the application to log meaningful information before it is terminated.

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

If WCF allowed an exception thrown by a service to be completely unhandled in this way it would mean that when the service is hosted in IIS the entire worker process would be terminated because a single request raised an exception - not a desirable outcome. As a result WCF doesn't leave exceptions thrown by services unhandled - this event will not be raised in this case.

If you want to log exceptions thrown by WCF services then take a look at the IErrorHandler interface instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...