【问题标题】:Win Forms Global Exception HandlingWinforms 全局异常处理
【发布时间】:2025-12-12 01:10:01
【问题描述】:

我正在尝试进行全局异常处理,我已经尝试了 3 种方法来执行此操作,但没有发现程序中某处引发的异常

    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

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();
    }
}

}

【问题讨论】:

  • “我正在使用单例类开始,对于每个视图(表单),我都会创建一个演示者,在其中创建视图” - 这就是 application.instance,应用程序.Instance.Start() 启动第一个演示者,该演示者启动第一个视图
  • “某处抛出异常” - 你能创建一个MCVE 吗?你是在说task unhandled exception吗?您可以edit您的问题添加缺失的信息。
  • 我只是在程序的某个地方做了一个“throw new Exception()”,所以我知道它是否有效,这就是我的意思,我会做一个 MCVE
  • 似乎缺少的一件事是Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  • 也试过了,还是没有发现错误

标签: c# winforms exception-handling singleton


【解决方案1】:

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

某处抛出异常

您必须处理以下事件:

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

【讨论】:

  • 如您所见,我已经在使用 ThreadException 和 UnhandledException,但它们不起作用,但谢谢。
  • 谢谢,“TaskScheduler.UnobservedTaskException”解决了我的问题。