0

各アクションでは、次のように記述します。

public ActionResult Foo()
{
    try
    {
        // Do something
    }
    catch(Exception ex)
    {
        Logger.Error("Foo()", ex);
    }
    ...
}

MVC3 には Global.asax に "RegisterGlobalFilters" を作成する機能があることを知っているので、カスタムの LogExceptionAttribute を書きたいと思います。これは:

  1. どの Actionがどの Exceptionを引き起こしたかの情報をログに記録します。私のLogger APIを呼び出す:

    Logger.Error(string actionName, Exception ex)

  2. 再び例外をスローします。web.config の CustomError がユーザー エラー ページを表示するようにします。

これを実装する方法は?

4

1 に答える 1

1

これを試して

  public class HandleErrorHmAttribute : HandleErrorAttribute
        {
            public override void OnException(ExceptionContext context)
            {
                base.OnException(context);
                new WebRequestErrorEventMvc("An unhandled exception has occurred.", this, 103005, context.Exception).Raise();
            }
        }

        public class WebRequestErrorEventMvc : WebRequestErrorEvent
        {
            public WebRequestErrorEventMvc(string message, object eventSource, int eventCode, Exception exception) : base(message, eventSource, eventCode, exception) { }
            public WebRequestErrorEventMvc(string message, object eventSource, int eventCode, int eventDetailCode, Exception exception) : base(message, eventSource, eventCode, eventDetailCode, exception) { }
       }
于 2012-11-06T04:40:33.877 に答える