首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Win窗体全局异常处理

Win窗体全局异常处理
EN

Stack Overflow用户
提问于 2017-10-05 09:16:58
回答 1查看 1.2K关注 0票数 1

我正在尝试做全局异常处理,我已经尝试了3种方法来做这件事,但是没有什么发现程序中某个地方抛出的异常

代码语言:javascript
复制
    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());

    }

这就是我已经尝试过的,没有什么例外。

而且我没有使用标准的application.run方法,而是使用一个单例类作为起点,对于每个视图(表单),我都会创建一个演示程序,在其中创建视图

有人知道如何处理此设置的全局异常吗?

还有,很抱歉我的英语很差

诚挚的问候

编辑: MVCE

代码语言:javascript
复制
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();
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2017-10-05 09:31:23

作为一个快速回答(因为我找不到所有这些副本),来处理

某个地方抛出的异常

您必须处理下列事件:

代码语言:javascript
复制
Application.ThreadException += ...

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

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

有关Application.ThreadExceptionAppDomain.CurrentDomain.UnhandledException之间的区别,请参见this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46581941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档