继承Exception类记录异常的所有后续出现

继承Exception类记录异常的所有后续出现

问题描述:

如果我想记录异常的所有出现在我的应用程序,到目前为止,

If I want to log all occurrences of exceptions throughout my application so far,

我应该继承Exception类并抛出该类的所有的异常,

should I inherit Exception class and throw all exception of that class,

它的构造将记录错误的详细信息。

whose constructor will log the error details..

或任何想法或建议?

根据应用的类型(ASP.NET/Console等),也有采取不同的方法。

Depending on the type of Application (ASP.NET/Console etc) there are different approaches to take.

有关Windows窗体应用程序,这是要走的路: -

For Windows Forms Applications this is the way to go: -

namespace YourNamespace
{
    static class Program
    {

        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException((Exception)e.ExceptionObject);
        }

        static void HandleException(Exception e)
        {
            //Handle the Exception here
        }

    }
}

在ASP.NET可以使用内的Application_OnError事件捕捉所有未处理的异常Global.asax中或通过挂钩用自己的HttpModule的Application.OnError事件。

In ASP.NET you can capture all Unhandled Exceptions by using the Application_OnError event within the Global.asax or by hooking up to the Application.OnError event with your own HttpModule.

您也可以使用这些第三方的异常处理程序中的一个。

You can also use one of these third party exception handlers.

ELMAH支持ASP.NET应用程序

ELMAH supports ASP.NET Applications

http://code.google.com/p/elmah/

Exceptioneer(我们建立这个 - 只是标记了我在该地区的利益)支持ASP.NET,控制台应用程序,Windows窗体Appliactions,WPF应用程序等。

Exceptioneer (We build this - just flagging up my interest in the area) supports ASP.NET, Console Applications, Windows Forms Appliactions, WPF Applications etc.

http://exceptioneer.com/