Web API Raygun error handler by Mark Seemann
Adding a Raygun error handler to ASP.NET Web API is easy.
In my Pluralsight course on a Functional Architecture with F#, I show you how to add a global error handler to an ASP.NET Web API site. The error handler you see in the course just saves the error as a text file in Azure BLOB storage, which is fine for a demo. For production software, though, you may want something a bit more sophisticated, like Raygun.
Here's how to add a Raygun exception filter to an ASP.NET Web API site:
let raygunHandler = { new System.Web.Http.Filters.IExceptionFilter with member this.AllowMultiple = true member this.ExecuteExceptionFilterAsync(actionExecutedContext, cancellationToken) = let raygunKey = CloudConfigurationManager.GetSetting "raygunKey" let raygunClient = Mindscape.Raygun4Net.RaygunClient raygunKey System.Threading.Tasks.Task.Factory.StartNew( fun () -> raygunClient.Send actionExecutedContext.Exception) }
This creates an Adapter from the ASP.NET Web API IExceptionFilter interface to a RaygunClient instance. As you can see, I use CloudConfigurationManager.GetSetting to get the Raygun API key from the configuration store.
The only remaining step is to add the error handler to an HttpConfiguration instance:
config.Filters.Add raygunHandler
That's it. Now you can use the Raygun service to manage your errors.