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 1 – Override the DataService<T>.HandleException method

 protected override void HandleException(HandleExceptionArgs args)
{
    eventProvider.WriteErrorEvent(
        this.GetType().Name,
        "Exception {0} status code {1}",
        args.Exception.Message,
        args.ResponseStatusCode);
}

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,

  1. Run the project and click the link Generate Error by querying bad resource  uses the link Services/Conference.svc/BadResource.
  2. Open IIS Manager
  3. Open the AppFabric Dashboard for the RESTWeb project

image

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.

image

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.

 

 [WebGet]
public string SayHello(string name)
{
    // Output a warning if name is empty
    if (string.IsNullOrWhiteSpace(name))
        eventProvider.WriteWarningEvent(
            "Conference SayHello", 
            "Warning - name is empty");
    else
        eventProvider.WriteInformationEvent(
            "Conference SayHello", 
            "Saying Hello to user {0}", 
            name);

    return "Hello " + name;
}

To see this warning

  1. Navigate to RESTWeb/Default.htm

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

  3. The call will succeed

  4. Open (or Refresh)  the AppFabric Dashboard for RESTWeb

  5. In the WCF Call History Click the Completed link

    image

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

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

image

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

  1. Check the connection string – it uses the database name AppFabricMonitoringDB

  2. 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

    SNAGHTMLbfb676

    Be sure to reboot after adding the DefaultAppPool to this group

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

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

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

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.

RESTWeb.zip