开发者

Inherit Exception class to log all subsequent occurrences of exception

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

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

whose constructor will log the error deta开发者_Go百科ils..

or any idea or suggestion???


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

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
        }

    }
}

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 supports ASP.NET Applications

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

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/


I work for CodeSmith Tools and I highly recommend taking a look at CodeSmith Insight. We use it in our commercial application and it has been a dream come true. We were using Fogbugz before and we evaluated a bunch of other products. It allows you to serialize whole objects as well as sends back a lot of extra information that helps in fixing bugs (Tracing, logs, tags, screenshot, user comments and much more). This cuts out time in solving the issue by having a ton of information at your finger tips as well as the ability to ask the user who encountered the error for more information.


Logging exception in the Exception's contructor is not a good idea.

Which application type you are using: web or windows?

If you are using asp.net then you can redirect your page to a common error page and log the error from that page. Asp.net also provides application_error event which fires in case of unhandled exception.

Usually, you use try/catch block to handle exception. You can log exception in the Catch block.

try
{

//your code

}

Catch(Exception ex)

{

 Log(ex);

}

You can use microsoft logging application block (or any other logging api) to log your exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜