This blog has moved to http://blogs.msdn.com/b/appfabric please update your links!
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.
protected override void HandleException(HandleExceptionArgs args) { eventProvider.WriteErrorEvent( this.GetType().Name, "Exception {0} status code {1}", args.Exception.Message, args.ResponseStatusCode); }
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,
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.
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
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
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.