Use WCF FaultException LogBook in CSLA

Juval Lowy, the author of Programming WCF, has a very good logbook sample application in the attached source code, which can log all the WCF FaultException into a database table.

To make it work with CSLA, I figured out there is a minor change needed. (The version I am working on in 3.0) The reason is WcfPortal actually doesn’t throw exception, instead, it casts the exception to an object returning to WcfProxy. This means, It’s WcfProxy at the client side throwing the exception. Apparently, Juval’s logbook can only monitor exceptions on Wcf Host, not at the client. I had to add an

throw ex;

into WcfPortal’s catching block to make CSLA framework can use this handy logBook tools.

    public WcfResponse Fetch(FetchRequest request)
      Csla.Server.DataPortal portal = new Csla.Server.DataPortal();
      object result;
        result = portal.Fetch(request.ObjectType, request.Criteria, request.Context);
      catch (Exception ex)
        result = ex;
      return new WcfResponse(result);

    public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context)
      ChannelFactory cf = new ChannelFactory(_endPoint);
      IWcfPortal svr = cf.CreateChannel();
      WcfResponse response =
        svr.Fetch(new FetchRequest(objectType, criteria, context));

      object result = response.Result;
      if (result is Exception)
        throw (Exception)result;
      return (DataPortalResult)result;

Thanks to Juval, this is almost exactly what our boss wants, the only thing missing is UserName and Client Machine information, and exception stack?

Most important, what’s the reason WcfPortal doesn’t throw exception? The explanation back from Rocky is, he wants to display the full stack of the error/exception so the exception on the server side is caught but not thrown out, instead this exception is passed to client as an parameter to the client side. Another ward, no exception will be thrown from CSLA at all! There is no way to use LogBook, unless we change CSLA framework.

Here is Andy’s solution from CLSA forum:

That’s what I do.. override OnDataPortalException.  You can do this in something like MyCompanyBusinessBase, and have all your classes inherit that subclass.  Rocky recommends creating such a subclass even if initially it doesn’t add any behavior at all, because someday you will.

One thing to watch is that if you’re doign different logging on server vs. client you need to check ExecutionLocation.  But it works great.

My exception handling actually builds up a message, drilling down to all InnerExceptions and emails the whole thing to me.  Very helpful to finding out where the exception is occuring.  If the email fails to send for some reason, my fallback is to log a message to the Windows Event Log.  This can be tricky to get going at first, because if you’re remoting you’ll need to manually add the Application Name to the registry so that your application can use that name (and you’ll want to do this to distingish between your remoting application and other errors).



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s