Win Forms全局异常处理 [英] Win Forms Global Exception Handling

查看:75
本文介绍了Win Forms全局异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行globalexceptionhandling,我已经尝试了3种方法来执行此操作,但是没有任何程序在程序中的某个地方引发异常

i'm trying to do globalexceptionhandling, i've already tried 3 methods to do this but nothing caught an exception thrown somewhere in the program

    static void Main()
    {
        System.Windows.Forms.Application.EnableVisualStyles();
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

        System.Windows.Forms.Application.ThreadException +=
            new System.Threading.ThreadExceptionEventHandler(Catch);

        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(Catch);

        try
        {
            Application.Instance.Start();
        }
        catch (Exception ex)
        {
            StackTrace st = new StackTrace(ex, true);
            StackFrame[] frames = st.GetFrames();

            List<string> errorList = new List<string>();
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());


            foreach (var frame in frames)
            {
                errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
            }

            SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());

            SyslogUdpSender sender = new SyslogUdpSender("localhost", 514);
            sender.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());
        }


    }

    static void Catch(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        StackTrace st = new StackTrace(e.Exception, true);
        StackFrame[] frames = st.GetFrames();

        List<string> errorList = new List<string>();
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());


        foreach (var frame in frames)
        {
            errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
        }

        SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());

        SyslogUdpSender send = new SyslogUdpSender("localhost", 514);
        send.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());

    }

    static void Catch(object sender, UnhandledExceptionEventArgs e)
    {
        StackTrace st = new StackTrace((Exception)e.ExceptionObject,                                                                                true);
        StackFrame[] frames = st.GetFrames();

        List<string> errorList = new List<string>();
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());


        foreach (var frame in frames)
        {
            errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
        }

        SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());

        SyslogUdpSender send = new SyslogUdpSender("localhost", 514);
        send.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());

    }

这是我已经尝试过的,没有发现

this is what i've already tried, nothing caught the exception.

,而且我没有使用标准的application.run方法,而是从一个单例类开始,对于每个视图(窗体),创建演示者并在其中创建视图

and i'm not using the standard application.run method, i'm using a singleton class to start from, where for every view(form) i have a presenter gets created in which the view gets created

有人知道如何使用此设置进行全局异常处理吗?

does anyone know how to do globalexception handling with this setup?

也,对不起我的英语不好

also, sorry for my bad english

最诚挚的问候

编辑:MVCE

namespace Application
{
static class Program
{
    static void Main()
    {
        System.Windows.Forms.Application.EnableVisualStyles();
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

        System.Windows.Forms.Application.ThreadException +=
            new System.Threading.ThreadExceptionEventHandler(Catch);

        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(Catch);

        try
        {
            Application.Instance.Start();
        }
        catch (Exception ex)
        {
            //do some catching
        }


    }

    static void Catch(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        //do some catching
    }

    static void Catch(object sender, UnhandledExceptionEventArgs e)
    {
        //do some catching
    }
}

public class Application
{
    private static Application _instance;

    private Application()
    {

    }

    public static Application Instance
    {
        get
        {
            return _instance ?? (_instance = new Application());
        }
    }

    internal void Start()
    {
        StartOverviewWindow();
    }

    private void StartOverviewWindow()
    {
        throw new Exception();
    }
}

}

推荐答案

作为一个快速答案(因为我找不到所有这些的重复项),来处理

As a quick answer (because I can't find duplicates for all of those), to handle


抛出异常的地方

an exception thrown somewhere

您必须处理以下事件:

Application.ThreadException += ...

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += ...

// tasks exceptions, add in app.config:
//  <runtime>
//      <ThrowUnobservedTaskExceptions enabled="true"/>
//  </runtime>
TaskScheduler.UnobservedTaskException += ...

之间的差Application.ThreadException AppDomain.CurrentDomain.UnhandledException 参见这个

这篇关于Win Forms全局异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆