Created
July 3, 2013 15:23
-
-
Save danshultz/5919292 to your computer and use it in GitHub Desktop.
.NET MVC Sentry logging in OnException
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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