1

web.config または global.asax ファイルを変更せずに未処理のエラーをキャプチャできるかどうかを知りたいです。

理想的には、WebActivator パッケージを使用して、次のようなものを用意します。

[assembly: WebActivator.PostApplicationStartMethod(typeof(MyProject.Initialize), "Start")]

namespace MyProject
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;

    internal static Initialize
    {
        public static void Start()
        {
              // this doesn't work
              HttpContext.Current.ApplicationInstance.Error += ApplicationInstance_Error;
        }

        // this never fires
        static void ApplicationInstance_Error(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

質問 #1: web.config または global.asax ファイルを変更せずに未処理のエラーをキャプチャすることは可能ですか?

質問 2: どのように?

更新:以下に回答を投稿しました。これが最善の方法かどうかはわかりません。コメントをお待ちしております。

4

1 に答える 1

0

この方法を使用して何かを機能させることができました:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Initialize), "Start")]

namespace MyProject
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;

    internal static Initialize
    {
        public static void Start()
        {
            GlobalFilters.Filters.Add(new WatchExceptionAttribute());
        }
    }

    public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            // do your thing here.
        }
    }
}
于 2012-04-17T02:24:43.193 に答える