C#全局异常处理程序

时间:2019-11-23 14:26:00

标签: c# visual-studio exception

这个问题已经被回答了上千次,但是尽管我已经尝试了一切,但似乎无法使我的全局异常处理程序正常工作。

我正在使用带有几种异步方法的Windows窗体。我所有的异步方法都包装在try catch块中,并进行了相应的处理。我还希望在每个代码块中都包含一个try catch块中的异常。

我有兴趣使用全局异常处理程序来捕获意外的异常,以便在安全关闭程序之前可以记录它们并使用户知道存在异常。

Main()中,我有以下内容:

    [STAThread]
    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
    static void Main()
    {
        crashLogInit = InitLogExceptions(); // sets global exception handlers and returns true if successful.
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainWindow = new MainWindow(); // My window
        LoadSaveManager.Init(); // initiates all relavent components for saving and loading data.
        if (isLoaded() && crashLogInit) // Loads data and checks it is successful, also checks global exception handlers are set.
        {
            RunProgram(); //Method which does Application.Run(MainWindow);
        }
        else
        {
            MessageBox.Show(Form.ActiveForm, "Error while starting application!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            CloseResources();
        }
    }

InitLogExceptions()中,我有以下内容:

 private static bool InitLogExceptions()
    {
        try
        {
            crashlogPath = Path.Combine(Directory.GetCurrentDirectory(), crashlogName);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            return true;
        }
        catch { return false; } 
    }

MainWindow()中,我有一个按钮,它触发一个单击事件,该事件引发一个ArgumentNullException,该事件被debbugers自己的异常处理程序捕获,但未被我的任何异常处理程序捕获,也没有被记录。

以下是我的异常处理程序的代码:

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        ShowException(sender, e.Exception, true);
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        ShowException(sender, e.ExceptionObject as Exception, true);
    }

    public static void ApplicationHandler(object sender, Exception e)
    {
        if (!crashLogInit) { InitLogExceptions(); }

        ShowException(sender, e, false);
    }

    public static void ApplicationHandler(object sender, Exception e, bool forceClose)
    {
        if (!crashLogInit) { InitLogExceptions(); }

        ShowException(sender, e, forceClose);
    }



    private static bool WriteToLog(object sender, Exception exception)
    {
        try
        {
            DateTime dateTime = DateTime.Now;

            StreamWriter writer = File.AppendText(crashlogPath);

            string date = dateTime.ToString("yyyy-MM-dd @ HH:mm:ss");

            writer.WriteLine(date + ":");

            writer.WriteLine();
            writer.WriteLine();

            writer.Write($" SENDER: {sender.GetType().ToString()} EXCEPTION MESSAGE: {exception.Message}");
            writer.WriteLine(exception.Source == null ? "" : $"SOURCE: {exception.Source}");
            writer.WriteLine();
            writer.WriteLine(exception.StackTrace == null ? exception.InnerException.StackTrace : exception.StackTrace);

            if (exception.GetType().IsSubclassOf(typeof(AppExceptions)))
            {
                writer.WriteLine();
                writer.WriteLine($"ORIGINAL MESSAGE: {exception.InnerException.Message} ORIGINAL SOURCE: {exception.InnerException.Source}");
            }

            writer.WriteLine();
            writer.WriteLine();

            writer.Close();

            return true;
        }
        catch (Exception)
        {
            return false;
        }


    }

    private static void ShowException(object sender, Exception e, bool forceClose)
    {
        StringBuilder output = new StringBuilder();

        if (WriteToLog(sender, e))
        {
            output.AppendLine($"Copied to Crashlog! Location of Crashlog file: {crashlogPath}");
            output.AppendLine();
            output.AppendLine("Send Crashlog to developer!");
        }
        else
        {
            output.AppendLine("Could not copy to Crashlog!");
        }
        output.AppendLine();
        output.AppendLine(forceClose ? "Application will now close!" : "Application will now continue at user's own risk!");


        MessageBox.Show(Form.ActiveForm, output.ToString(), e.Message.ToUpper() + "!", MessageBoxButtons.OK, MessageBoxIcon.Error);

        if (forceClose)
        {
            Environment.FailFast(e.Message, e);
        }
    }

在单击并单击按钮时会抛出此简单异常,但在其他地方正常工作并被记录的异常。我在做什么错了?

0 个答案:

没有答案