0

MVCアプリケーションで、標準のMVCエラーページを表示し、Microsoft例外処理アプリケーションブロックを使用して例外を処理するにはどうすればよいですか?

web.configファイルを次のように更新しました

これにより、例外が発生するとトラブルビューにリダイレクトされます。ただし、例外は例外処理アプリケーションブロック(以下のコード)では処理されなくなりました。ExceptionPolicy.HandleException(ex、 "AllExceptions"、out errorToThrow);

エラービューを表示し、例外を処理するにはどうすればよいですか?ELMAHは使いたくない。

4

2 に答える 2

1

ロギングとエラー処理には、Log4netとHttpHandlerを使用します。次のサンプルコードを参照してください。ExceptionHandlerそして、同じ属性でコントローラーを飾ります。( [ExceptionHandler] public class BillsController : Controller { }

public class ExceptionHandler : HandleErrorAttribute
{
    private static readonly ILog log = LogManager.GetLogger(typeof(ExceptionHandler));

    public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
    {
        log.Debug("******** ExceptionHandler.OnException() *****************");

        base.OnException(filterContext);
        Exception ex = filterContext.Exception;
        if (filterContext.Exception is BusinessException)
        {
            log.Debug("<<<<Inside On BusinessException....>>>>" + DateTime.Now);
            //log.Debug("*** Error Getting From **** " + LOGIN_DETAILS.LoginUserName +
            //          "(UserId = " + LOGIN_DETAILS.LoginUserID + ")" + "Time =" + DateTime.Now);


            BusinessException _BusinessException =
                (BusinessException)filterContext.Exception;

            StringBuilder errormsg = new StringBuilder();
            foreach(MessageInfo msg in _BusinessException.ErrorMessageList)
            {
                errormsg.AppendLine(msg.LocalizedMessage);
            }

            log.Error("<<<<--------BusinessException-------->>>> : Exception Details -----"+ errormsg.ToString());
            //filterContext.ExceptionHandled = true;

            filterContext.ExceptionHandled = true;
            RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Account", action = "Logout", message = filterContext.Exception.Message })).VirtualPath;
            filterContext.HttpContext.Response.Redirect(url, true);
        }
        else
        {
             log.Error("Exception Details ---- " +
               "MESSAGE: " + ex.Message +
               "\nSOURCE: " + ex.Source +
               "\\Controller: " + filterContext.Controller.ToString() +
               "\nTARGETSITE: " + ex.TargetSite +
               "\nSTACKTRACE: " + ex.StackTrace,
               ex);

            filterContext.ExceptionHandled = true;
            RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Exception", action = "Default" })).VirtualPath;
            //string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "exception", action = "Default", message = filterContext.Exception.Message })).VirtualPath;
            filterContext.HttpContext.Response.Redirect(url, true);
        }

    }
}
于 2012-10-10T06:19:04.740 に答える
1

web.configファイルを変更して、カスタムエラーを表示します。これにより、エラーページを表示できます。

次に、例外を処理するために、この関数をglobal.asaxに追加します。

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    //handle the exception
}
于 2012-10-10T06:35:19.100 に答える