29

Is there a way to globally handle exceptions for a Windows Service? Something similar to the following in Windows Forms applications:

Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException);
Jeremy Odle
  • 333
  • 2
  • 4
  • 7
  • 2
    The most relevant answer for Windows Services can be found here: http://stackoverflow.com/a/5117790/986720 It explains why the exception is not caught by AppDomainUnhandledException handler – C-F Jan 11 '16 at 19:57

2 Answers2

28

Have you tried

AppDomain.CurrentDomain.UnhandledException

This will fire for unhandled exceptions in the given domain no matter what thread they occur on. If your windows service uses multiple AppDomains you'll need to use this value for every domain but most don't.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    If your service runs using a `System.Timers.Timer` timer and does periodic work in the `Elapsed` event then unhandled exceptions raised in that event will not get seen by the `AppDomain.CurrentDomain.UnhandledException` event. The timer just swallows them and does nothing with the exception. – Scott Chamberlain Jan 26 '17 at 00:18
  • 1
    I know this is an old thread. Does the same apply to System.Threading.Timers? – Bill Greer Oct 09 '17 at 20:03
  • @BillGreer, yes, unfortunately it also apply to SystemThreading.Timers. – jacktric Jul 04 '18 at 13:48
23

Here is some pretty robust code we advise people to use when they're implementing http://exceptioneer.com in their Windows Applications.

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
        }

    }
}

Thanks,

Phil.

Plip
  • 1,040
  • 8
  • 9
  • 4
    Where is "Application" in relation to Windows Services? – ferventcoder Nov 16 '15 at 16:06
  • 1
    Did you get answer for your comment? Where is "Application" – HaBo May 16 '16 at 08:02
  • 6
    `Application` (https://msdn.microsoft.com/en-us/library/system.windows.forms.application(v=vs.110).aspx) and much of the code above is specific to WinForms and not relevant to Windows Services. `AppDomain.CurrentDomain.UnhandledException` is the important one for Services. See also http://stackoverflow.com/questions/2456819/how-can-i-set-up-net-unhandledexception-handling-in-a-windows-service for exception handling discussion. – Rory Jun 05 '16 at 14:04