Monitoring WCF Data Service Exceptions with Windows Server AppFabric

After my previous post about how you can monitor service calls with AppFabric, several people asked about how you can capture errors with AppFabric. Unfortunately, the default error handling behavior of WCF Data Services does not work well with AppFabric. Though it does return HTTP status codes to the calling application to indicate the error, AppFabric will not record these as exceptions as it would normally.

To work around this behavior, you can simply add code to handle exceptions and report the error to AppFabric using the same WCFEventProvider I showed in the previous post. I’ve cleaned up and modified the code a bit to handle errors as well.

I’ve attached the sample code to this blog post. It includes the SQL script you will need to create the database.

Step 2 – Invoke the service with an invalid URI

If you invoke the service with an invalid URI it will return an HTTP 404 (Not Found) exception. It might be nice to know if you are getting a great deal of invalid URIs thrown at your service. I’ve modified the Default.htm file to include a link that will yield an error. To see the error,

Run the project and click the link Generate Error by querying bad resource uses the link Services/Conference.svc/BadResource.

Open IIS Manager

Open the AppFabric Dashboard for the RESTWeb project

You will notice that this one call to the service is recorded as both a Completed call and also an Error. This is a side effect of the way that WCF Data Services report the error, this will be corrected in a future release.

To get specifics about the error, just click the link for Errors and click on the Errors tab to see the exception text.

Step 2 – Provide Warnings for ServiceMethods

There may be cases where you want to provide a warning in the AppFabric log. In the sample code, I’ve included a service method on the data service that accepts a name parameter. If no name is provided, the code records a warning.

Click the link Generate warning with empty name to SayHello which passes a name with Whitepsace.

The call will succeed

Open (or Refresh) the AppFabric Dashboard for RESTWeb

In the WCF Call History Click the Completed link

Right click on the top OperationCompleted event and select View All Related Events

Look for the warning event. The payload will contain the warning text.

Step 3 – Read The Monitoring Database

Finally I thought you might like to see how you can read these monitoring events from the monitoring database with a web app. I’ve added the ShowEvents.aspx page which will display events from the Conference service.

To make this work

Check the connection string – it uses the database name AppFabricMonitoringDB

The IIS APPPOOL\DefaultUser will need to be granted permission to read from this database. To do this, just add it to the Local Security Group AS_Observers

Be sure to reboot after adding the DefaultAppPool to this group

Next I created an Entity Framework Model that includes just the WCFEvent View.

Then I dropped a ListView on the page and wired it up to the dataset.

Click on the Show Monitoring Events from Conference Service link to see it

Summary

With a little bit of code in your WCF DataService, AppFabric can provide some very useful monitoring information. And because AppFabric collects this information across the server farm, all the servers will log events in the monitoring database.