none
How to Catch the Application_Error in a Windows Service? RRS feed

  • Question

  • I have created a Windows Service which has start and stop methods and some other functionalities.

    I need to capture the Application_Error globally throughout the windows service when it happens. Is that possible?

    Something like Application_Error in global.ascx file of an ASP.NET app.
    Tuesday, November 24, 2009 3:10 PM

Answers

All replies

  • You can allways implement a Try Catch on the main worker therad.
    Tuesday, November 24, 2009 7:21 PM
  • The main worker you mean Start and Stop?

    There would be events which happen after Start runs such as when a message is received from another application MessageReceived(object sender, eventargs ...)

    What I try to avoid is to put try and catch everywhere. Any application level error handler?
    Wednesday, November 25, 2009 7:31 AM
  • Did you consider subscribing to UnhandledExcetpion event? Is that what you are looking for?

    -Karel
    Wednesday, November 25, 2009 5:37 PM
    Moderator
  • :) That seems to be exactly what I am looking for.

    My only concern is that I am using a multithreaded environment where I am running multiple background workers and threads.

    I know that multiple threads can run in one AppDomain but when does a new AppDomain created exactly?

    Is it correct that all my threads are running in the default AppDomain that I have so that subscribing to UnhandledException of the CurrentDomain would be sufficient? (I have not created any new AppDomain manually)

    Thanks,
    Wednesday, November 25, 2009 5:52 PM
  • Try the following... 

    static class Program
        {
         static void Main()
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.UnhandledException += new UnhandledExceptionEventHandler(ApplicationErrorHandler);
                RunService();
            }
    
    static void ApplicationErrorHandler(object sender, UnhandledExceptionEventArgs args)
            {
                Exception e = (Exception)args.ExceptionObject;
                // Log the exception here            
            }
    }
    ------------------------------------------------------------
    Please remember to mark the replies as answers if they help
    -Saju
    Friday, November 27, 2009 5:28 AM
  • Yeah, Karel mentiond this.

    My question is:

    "Is it correct that all my threads are running in the default AppDomain that I have so that subscribing to UnhandledException of the CurrentDomain would be sufficient? (I have not created any new AppDomain manually)"
    Friday, November 27, 2009 10:37 AM
  • AppDomain is a leight-weight process. Unless your app creates a new AppDomain explicitly, there will be only one - the default AppDomain. (That is not truth in SilverLight which has 2 AppDomains by default - System and User)

    Check out more info about AppDomains:
        http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspx
        http://msdn.microsoft.com/en-us/library/system.appdomain.aspx

    Unless you do something special or use some special libraries, you should be fine subscribing to the CurrentDomain's UnhandledException.

    -Karel
    Friday, November 27, 2009 11:45 PM
    Moderator