簡體   English   中英

C#未處理的異常捕獲器

[英]c# unhandled exceptions catcher

我們有一個包含多個項目的VS2013解決方案(本來是VS2012解決方案,但不相關)。 一些類庫。 一些MVC應用程序。 一些WinForm應用程序。 一些comandline應用程序。 我們需要一個可以在所有項目中使用的類,以捕獲未處理的異常並將它們記錄到可配置的目標中(特定的應用程序可以決定是否要記錄到平面文件,事件日志或db)。 想法是,在啟動時,應用程序將調用靜態方法或實例化對象,然后將所有未處理的異常神奇地記錄到應用程序的其余部分。

我認為這可以解決問題(盡管第一次嘗試只是記錄到一個平面文件中):

    public class InfLogger
{
    public string LogFile { get; set; }
    public InfLogger()
    {
        System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ThreadExceptionHandler);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionsHandler);
        LogFile = "C:\\Infinedi.log";
    }

    public void ThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        try
        {
            if (File.Exists(LogFile))
            {
                File.AppendAllText(LogFile, e.Exception.Message + "\n");
            }
            else
            {
                File.WriteAllText(LogFile, e.Exception.Message + "\n");
            }
        }
        catch (Exception ex)
        {

        }
    }

    public void UnhandledExceptionsHandler(object sender, UnhandledExceptionEventArgs e)
    {
        try
        {
            if (File.Exists(LogFile))
            {
                File.AppendAllText(LogFile, (e.ExceptionObject as Exception).Message + "\n");
            }
            else
            {
                File.WriteAllText(LogFile, (e.ExceptionObject as Exception).Message + "\n");
            }
        }
        catch (Exception ex)
        {

        }
    }
}

}

但沒有喜悅。

在我用來嘗試對其進行測試的MVC應用程序中,我實例化了InfLogger對象,然后有目的地創建超出范圍的未處理異常的數組索引。 但是記錄器不會觸發任何一個事件處理程序。 :(

關於如何做到這一點的任何建議?

我已經做過一些研究,實際上這對於控制台應用程序和Winforms確實有效。 但不是MVC。 嗯...不知道為什么。 但是,我發現了一個新問題。 這是使用代碼的地方。

    namespace TestApp
    {
class Program
{
    static void Main(string[] args)
    {
        InfLogger logger = new InfLogger();
        int[] a = { 0, 1, 2 };
        int y = a[6];
    }
}
}

記錄器確實捕獲到異常。 但是執行只是掛在一行上:int y = a [6]。 它只是繼續執行該行,拋出異常,一遍又一遍地捕獲未處理的異常。 因此,現在我需要知道如何獲得執行,以實際越過引發異常的行。

我在Program.cs文件中調用一個方法來設置未處理的異常方法。 這是MSDNApplication.SetUnhandledExceptionMode方法的完整說明。

// ....
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        SetUnhandledExceptionHandler();
        // ...

#region -- SetUnhandledExceptionsHandler() Method --
    /// <summary>
    /// Causes all unhandled exceptions to be handled by the application.
    /// This will not prevent the application from being forced to close, but
    /// does give a chance to log the error.
    /// </summary>
    public static void SetUnhandledExceptionHandler()
    {
        // Add an exception handler for all UI thread exceptions
        Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

        // Set the unhandled exception mode to force all Windows Forms errors to
        // Go through this handler
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // Add the event handler for handling non-UI thread exceptions
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    }
#endregion

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM