Logging Errors In OWIN Applications Using Raygun.io
Note
2014-12-11: a new beta release of Raygun.Owin is available – more details over here
Recently I’ve been looking into analytics and error logging services. For error logging I’ve settled on using Raygun.io. Not only was the service simple to use, but they also provide a library called Raygun4Net which is hosted on GitHub as well as a NuGet package making getting started real easy.
I quickly found out though that this library references System.Web and expects an HttpContext when logging errors. Since my project runs on OWIN I don’t have an HttpContext to pass in, nor am I referencing System.Web. As a result I wrote up a piece of OWIN middleware called Raygun.Owin which you can find on GitHub and as a NuGet package.
Setting Up Raygun.Owin
First you will need to install Raygun.Owin from NuGet:
Install-Package Raygun.Owin
Next you will need to add your API key to your config file’s appSettings
like so:
<appSettings> <add key="raygun:apiKey" value="abc123" /></appSettings>
Then you will need to add the RaygunUnhandledExceptionMiddleware
at the beginning of your application’s setup:
public class Startup{ public static void Configuration(IAppBuilder app) { app.UseRaygunUnhandledExceptionLogger();
app.UseCassette(); app.UseNancy(); }}
If you would also like to track unhandled requests you can add the RaygunUnhandledRequestMiddleware
at the very end of the pipeline like so:
public class Startup{ public static void Configuration(IAppBuilder app) { app.UseRaygunUnhandledExceptionLogger();
app.UseCassette(); app.UseNancy(options => { // NOTE: requires nancy v0.20.0 or newer // this will allow 404 errors to pass through to the next // piece of middleware options.PassThroughWhenStatusCodesAre(HttpStatusCode.NotFound); });
app.UseRaygunUnhandledRequestLogger(); }}
Using with Web API
If you’re using Web API you’ll need to add an exception filter that will save exceptions to the current OWIN environment using the key webapi.exception
.
This is because Web API swallows exceptions in order to gracefully return their details in the call response.
The simplest way to do this is as follows:
public class Startup{ public static void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.Filters.Add(new OwinRequestExceptionLoggerAttribute()); app.UseWebApi(config); }}
public class OwinRequestExceptionLoggerAttribute : ExceptionFilterAttribute{ public override void OnException(HttpActionExecutedContext actionExecutedContext) { var owinContext = actionExecutedContext.ActionContext.Request.GetOwinContext(); owinContext.Set("webapi.exception", actionExecutedContext.Exception); }}
Using Raygun.io with JavaScript
I haven’t tried logging errors on the client side yet, though that is my next step since I’m working with AngularJS. As a starting point Phillip Haydon has a blog post on setting this up, as well as a few other posts on using Raygun. I’d recommend checking them out as well as his Nancy.Raygun project if you’re using Nancy but not OWIN.