获取一般 Windows 应用程序错误

发布于 2024-12-15 18:51:00 字数 110 浏览 1 评论 0 原文

如何获取 Windows 应用程序或 Windows 服务中发生的一般错误,例如 HttpApplication 中的 Application_Error

How can I get generic occurred errors in windows application or windows services like Application_Error in HttpApplication

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-12-22 18:51:00

您可以使用以下事件来捕获 Windows 窗体应用程序和 .Net 服务中的异常:

AppDomain.FirstChanceException

每当给定 AppDomain 中出现异常时,即使稍后处理该事件,也会触发该事件。这几乎肯定不是您想要的,但我想为了完整性而将其包括在内。

请注意,这是一个针对每个应用程序域的事件 - 如果您使用多个应用程序域,那么您需要为每个应用程序域处理此事件。如果您只有一个 AppDomain(更有可能),则以下内容将处理此事件:

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

static void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
    throw new NotImplementedException();
}

AppDomain.UnhandledException

每当给定 AppDomain 中存在未处理异常时,就会触发此事件。同样,这是一个针对每个 AppDomain 的事件,其连接方式与 FirstChanceException 事件类似。

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    throw new NotImplementedException();
}

您可以随时挂接这两个事件,但是您可能希望尽快执行此操作,否则在挂接事件处理程序之前可能会引发异常。 Program 类中 Main 方法的开头通常是执行此操作的好地方。

请注意,在 Windows 窗体应用程序中,当您希望它触发时,此事件可能不会被触发,因为 Windows 窗体应用程序中未处理的异常的处理方式不同(由 Windows 窗体基础结构),因此有时不会作为 AppDomain 中的未处理异常进行传播。

看看 为什么我的 Catch 块仅在 Visual Studio 中调试时运行?

Application.ThreadException

(only for Windows Forms applications)

当 Windows 窗体应用程序中引发未处理的异常,然后 Windows 窗体基础结构捕获该异常时,就会发生这种情况。您可能应该使用它而不是 AppDomain.UnhandledException 来捕获 Windows 窗体应用程序中未处理的异常:

Application.ThreadException += Application_ThreadException;

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    throw new NotImplementedException();
}

同样,您希望尽快连接它 - Main 的开始Program 类中的 > 方法通常是执行此操作的好地方。

摘要

请注意,它们都与 ASP.Net 应用程序的 Application_Error 方法完全不同,但是如果您正在创建 Windows 窗体应用程序,那么您可能 >想要使用Application.ThreadException并且如果您正在创建Windows服务,那么您可能想要AppDomain.UnhandledException

You can use the following events to trap exceptions in Windows Forms applications and .Net Services:

AppDomain.FirstChanceException

This event is triggered whenever there is an exception in a given AppDomain, even if the event is later handled. This is almost certainly not what you want but I thought I'd include it for completeness.

Note that this is a per-AppDomain event - if you use multiple AppDomains then you need to handle this event for each one. If you only have a single AppDomain (more likely) then the following will handle this event:

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

static void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
    throw new NotImplementedException();
}

AppDomain.UnhandledException

This event is triggered whenever there is an unhandled exception in a given AppDomain. Again this is a per-AppDomain event and is wired up in a similar way to the FirstChanceException event.

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    throw new NotImplementedException();
}

You can hook these two events at any point you like however you probably want to do this as soon as possible otherwise exceptions might be thrown before you hook up your event handler. The start of the Main method in your Program class is normally a good place to do this.

Note that in a Windows Forms application this event might not get triggered when you would otherwise expect it to as unhandled exceptions in Windows Forms applications are handled differently (by the Windows Forms infrastructure) and so are sometimes not propogated up as unhandled exceptions in the AppDomain.

Take a look at Why is my Catch block only running while Debugging in Visual Studio?

Application.ThreadException

(only for Windows Forms applications)

This occurs when an unhandled exception is thrown in a Windows Forms application which is then caught by the Windows Forms infrastructure. You should probably be using this instead of AppDomain.UnhandledException to catch unhandled excpetions in Windows Forms applications:

Application.ThreadException += Application_ThreadException;

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    throw new NotImplementedException();
}

Again you want to hook this up as soon as possible - the start of your Main method in the Program class is normally a good place to do this.

Summary

Note that none of them are exactly like the Application_Error method of an ASP.Net application however if you are creating a Windows Forms application then you probably want to use Application.ThreadException and if you are creating a Windows Service then you probably want AppDomain.UnhandledException

聊慰 2024-12-22 18:51:00

您可以像这样订阅 UnhandledException 事件:

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

在您的代码中有一个方法,如下所示:

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // DoSomething
}

You can subscribe to the UnhandledException event like this:

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

An have a method in your code like this:

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // DoSomething
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文