Windows服务中未处理的exception

我创建了一个运行多个任务的.net c#windows服务。 我在其中包含exception处理但我想设置一个全局处理程序来捕获Windows服务中未处理的exception,我该怎么做?

我在其中包含exception处理,但我想设置一个全局处理程序来捕获Windows服务中未处理的exception

可以使用AppDomain.UnhandledException 。 但是,在您的情况下,您对服务的整个调用可以包含在try / catch块中。 由于您没有提供有关此未处理exception的详细信息,因此我认为在这种情况下您的正确路径是让服务崩溃

 try { MainCallToYourService(); } catch (Exception) { //it's probably too late to do anything useful here, try to log and die } 

请记住,这样做的问题在于,在很多情况下,应用程序的状态在引发此事件时已损坏。 你最好的办法就是尝试登录并离开。