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
422 views
in Technique[技术] by (71.8m points)

.net - How do I create a global exception handler for a WCF Services?

I want to log all exceptions server side.

In ASP.NET I write something like this in Global.asax.cs, but will this work for a WCF service, too?

public class Global : HttpApplication
{
    protected void Application_Error(object sender, EventArgs e)
    {
        Exception unhandledException = Server.GetLastError();

        //Log exception here
        ...
    }
} 

UPDATE: I don't want to have a try...catch for every [OperationContract] in my .svc file. I short... I want to make sure that all exception that my service throws is logged by log4net. I'm not talking about how the client handles exception.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can create a WCF error-logger by implementing IErrorHandler and associating it with the service; typically (for logging) you would return false from HandleError (allowing other handlers to execute), and log the error either in HandleError (using the Exception) or in ProvideFault (using the ref Message fault).

I apply this handler by writing a custom behavior (inheriting from BehaviorBase), which (in ApplyDispatchBehavior) adds the error-handler to endpointDispatcher.ChannelDispatcher.ErrorHandlers if it isn't already there.

The behavior can be applied via configuration.


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

...