public class JgoExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
Utility.Logger.log.Error(
"Type:" + actionExecutedContext.Exception.GetType().ToString() +
":Url:" + actionExecutedContext.Request.RequestUri +
":Body:" + GetBodyFromRequest(actionExecutedContext) +
//":Message:" + actionExecutedContext.Exception.Message +
//":StackTrace:" + actionExecutedContext.Exception.StackTrace +
":BaseMessage:" + actionExecutedContext.Exception.GetBaseException().Message +
":BaseStackTrace:" + actionExecutedContext.Exception.GetBaseException().StackTrace
);
//Log Exception
//Utility.Logger.log.Error(actionExecutedContext.Exception.GetType().ToString() + ":" + actionExecutedContext.Exception.Message + ":" + actionExecutedContext.Exception.StackTrace + ":" + actionExecutedContext.Exception.StackTrace);
base.OnException(actionExecutedContext);
}
private string GetBodyFromRequest(HttpActionExecutedContext context)
{
string data;
using (var stream = context.Request.Content.ReadAsStreamAsync().Result)
{
if (stream.CanSeek)
{
stream.Position = 0;
}
data = context.Request.Content.ReadAsStringAsync().Result;
}
return data;
}
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
Utility.Logger.log.Error(
"Type:" + actionExecutedContext.Exception.GetType().ToString() +
":Url:" + actionExecutedContext.Request.RequestUri +
":Body:" + GetBodyFromRequest(actionExecutedContext) +
//":Message:" + actionExecutedContext.Exception.Message +
//":StackTrace:" + actionExecutedContext.Exception.StackTrace +
":BaseMessage:" + actionExecutedContext.Exception.GetBaseException().Message +
":BaseStackTrace:" + actionExecutedContext.Exception.GetBaseException().StackTrace
);
//Log Exception
//Utility.Logger.log.Error(actionExecutedContext.Exception.GetType().ToString() + ":" + actionExecutedContext.Exception.Message + ":" + actionExecutedContext.Exception.StackTrace + ":" + actionExecutedContext.Exception.StackTrace);
base.OnException(actionExecutedContext);
}
private string GetBodyFromRequest(HttpActionExecutedContext context)
{
string data;
using (var stream = context.Request.Content.ReadAsStreamAsync().Result)
{
if (stream.CanSeek)
{
stream.Position = 0;
}
data = context.Request.Content.ReadAsStringAsync().Result;
}
return data;
}
}
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Filters.Add(new JgoExceptionFilterAttribute());
本文介绍了一个自定义的异常过滤器属性 JgoExceptionFilterAttribute 的实现方式,该属性用于 ASP.NET Web API 中捕获并记录 HTTP 操作过程中发生的异常。文中详细展示了如何获取异常类型、请求 URL、请求体内容以及异常的基本信息。
477

被折叠的 条评论
为什么被折叠?



