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.
You can create a WCF error-logger by implementing
IErrorHandlerand associating it with the service; typically (for logging) you would returnfalsefromHandleError(allowing other handlers to execute), and log the error either inHandleError(using theException) or inProvideFault(using theref Message fault).I apply this handler by writing a custom behavior (inheriting from
BehaviorBase), which (inApplyDispatchBehavior) adds the error-handler toendpointDispatcher.ChannelDispatcher.ErrorHandlersif it isn’t already there.The behavior can be applied via configuration.