Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created July 3, 2013 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danshultz/5919292 to your computer and use it in GitHub Desktop.
Save danshultz/5919292 to your computer and use it in GitHub Desktop.
.NET MVC Sentry logging in OnException
public RavenClient raven
{
get
{
var ravenKey = "raven_key";
var raven = new RavenClient(ravenKey);
return raven;
}
}
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
}
// If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled)
{
return;
}
Exception exception = filterContext.Exception;
// Ingnore Exceptions that generate http status codes less than 500
var httpCode = new HttpException(null, exception).GetHttpCode();
if (httpCode < 500)
{
return;
}
// Lambda for pretty output of NameValueCollections
Func<NameValueCollection, string> formatCollection =
collection =>
{
return String.Join("\r\n", collection.AllKeys.SelectMany(key =>
{
return collection.GetValues(key).Select(value => key + "=" + value);
}
));
};
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
var tags = new Dictionary<string, string> { { "controller", controllerName } };
var data = new Dictionary<string, object>();
data["headers"] = formatCollection(Request.Headers);
data["controller_name"] = controllerName;
data["action_name"] = actionName;
data["url"] = Request.Url;
data["query_string"] = formatCollection(Request.QueryString);
data["http_action"] = Request.RequestType;
data["data"] = Request.Form;
data["view_data"] = new ViewDataDictionary<HandleErrorInfo>(model);
data["temp_data"] = filterContext.Controller.TempData;
raven.CaptureException(exception, tags: tags, extra: data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment