Skip to content

Instantly share code, notes, and snippets.

@jmarmolejos
Last active August 29, 2015 13:58
Show Gist options
  • Save jmarmolejos/10069215 to your computer and use it in GitHub Desktop.
Save jmarmolejos/10069215 to your computer and use it in GitHub Desktop.
// RaygunErrorHandlerAttribute.cs
using System.Web.Mvc;
using Microsoft.WindowsAzure;
using Mindscape.Raygun4Net;
namespace MyProject.Attributes
{
public class RaygunErrorHandlerAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
var apiKey = CloudConfigurationManager.GetSetting("RaygunApiKey");
var httpCtx = filterContext.RequestContext.HttpContext;
if (!httpCtx.Request.IsLocal)
{
// Local requests should not notify Railgun, a long debugging session might cap our account pretty soon
var exception = filterContext.Exception;
new RaygunClient(apiKey).Send(exception);
}
}
}
}
// RaygunErrorHandlerHttpAttribute.cs
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http.Filters;
using Microsoft.WindowsAzure;
using Mindscape.Raygun4Net;
namespace MyProject.Attributes
{
public class RaygunErrorHandlerHttpAttribute : IExceptionFilter
{
public bool AllowMultiple { get; private set; }
public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
var apiKey = CloudConfigurationManager.GetSetting("RaygunApiKey");
var httpCtx = HttpContext.Current;
return Task.Factory.StartNew(() => LogError(actionExecutedContext, httpCtx, apiKey));
}
private static void LogError(HttpActionExecutedContext actionExecutedContext, HttpContext httpCtx, string apiKey)
{
if (actionExecutedContext.Exception != null && !httpCtx.Request.IsLocal)
{
// Local requests should not notify Railgun, a long debugging session might cap our account pretty soon
var exception = actionExecutedContext.Exception;
new RaygunClient(apiKey).Send(exception);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment