问题
I have created a .net c# windows service that runs multiple tasks. I included exception handling in it but I would like to set up a global handler to catch unhandled exceptions in the windows service, how can I do this?
回答1:
I included exception handling in it but I would like to set up a global handler to catch unhandled exceptions in the windows service
You could use AppDomain.UnhandledException
. In your case however, your entire call to your service can be wrapped in a try/catch block. Since you haven't provided details for what you plan to do with this unhandled exception, your correct path I think in this case is to let the service crash.
try
{
MainCallToYourService();
}
catch (Exception)
{
//it's probably too late to do anything useful here, try to log and die
}
Keep in mind though that the problem in doing this is that in a lot of cases, your application's state is corrupt by the time this event is raised. Your best bet is to try and log it and get out.
来源:https://stackoverflow.com/questions/10609443/unhandled-exceptions-in-a-windows-service