즉, 이동하는 방법이처럼 HandleErrorAttribute을 확장 할 수 있습니다 : 당신이 필요하지 않은 경우
public class OncHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
// Elmah-Log only handled exceptions
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
if (context.HttpContext.Request.IsAjaxRequest())
{
// if request was an Ajax request, respond with json with Error field
var jsonResult = new ErrorController { ControllerContext = context }.GetJsonError(context.Exception);
jsonResult.ExecuteResult(context);
context.ExceptionHandled = true;
}
else
{
// if not an ajax request, continue with logic implemented by MVC -> html error page
base.OnException(context);
}
}
}
가 ELMAH 로깅 코드 라인을 제거합니다. 내 컨트롤러 중 하나를 사용하여 오류 및 컨텍스트를 기반으로 json을 반환합니다. 다음은 샘플입니다.
public class ErrorController : Controller
{
public ActionResult GetJsonError(Exception ex)
{
var ticketId = Guid.NewGuid(); // Lets issue a ticket to show the user and have in the log
Request.ServerVariables["TTicketID"] = ticketId.ToString(); // Elmah will show this in a nice table
ErrorSignal.FromCurrentContext().Raise(ex); //ELMAH Signaling
ex.Data.Add("TTicketID", ticketId.ToString()); // Trying to see where this one gets in Elmah
return Json(new { Error = String.Format("Support ticket: {0}\r\n Error: {1}", ticketId, ex.ToString()) }, JsonRequestBehavior.AllowGet);
}
위의 티켓 정보를 일부 추가하면 무시할 수 있습니다. 때문에 필터가 구현되는 방식 (기본 HandleErrorAttributes를 확장)에 우리는 HandleErrorAttribute 글로벌 필터에서 다음 제거 할 수 있습니다
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new GlobalAuthorise());
filters.Add(new OncHandleErrorAttribute());
//filters.Add(new HandleErrorAttribute());
}
이 기본적이다. 더 자세한 정보는 my blog entry을 읽을 수 있지만 위의 아이디어로 충분합니다.
나는 이것을 시도했지만 HandleJsonErrorAttribute OnException 메서드를 호출 할 때 action이 속한 Controller 클래스의 HandleErrorAttribute가 true 일 때 filterContext.ExceptionHandled 속성은 항상 true입니다. 액션 메소드 handleerror가 우선시되고 먼저 호출되어야하지 않습니까? –