Csharp/C#教程:Windows服务的全局exception处理程序?分享


Windows服务的全局exception处理程序?

有没有办法全局处理Windows服务的exception? 类似于Windows窗体应用程序中的以下内容:

Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException); 

这是一些非常强大的代码,我们建议人们在他们的Windows应用程序中实现https://exceptioneer.com时使用。

 namespace YourNamespace { static class Program { [STAThread] static void Main() { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { HandleException(e.Exception); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { HandleException((Exception)e.ExceptionObject); } static void HandleException(Exception e) { //Handle your Exception here } } } 

谢谢,

菲尔。

你有没有尝试过

 AppDomain.CurrentDomain.UnhandledException 

这将触发给定域中的未处理exception,无论它们出现在哪个线程上。 如果您的Windows服务使用多个AppDomain,则需要为每个域使用此值,但大多数不需要。

上述就是C#学习教程:Windows服务的全局exception处理程序?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1029711.html

(0)
上一篇 2022年1月12日
下一篇 2022年1月12日

精彩推荐