4

I've setup the UnhandledException to log any exceptions before my service crashes.

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(unhandledException); 

I'm testing this by throwing and unhandled exception.

throw new Exception("TestExcepion"); private void unhandledException(object sender, UnhandledExceptionEventArgs e) { // Logs to Log4Net but its not reaching here. Log.Info("Unhandled Exception: " + (e.ExceptionObject as Exception).ToString()); } 

I'm attempting to log this within my unhandledException method using Log4Net but im getting nothing.

Why isn't the exception being caught?

1
  • Post more of your code here. Commented Sep 3, 2013 at 11:44

1 Answer 1

4

It is probably that your throwing code is before use set AppDomain.CurrentDomain.UnhandledException or because you put it in OnStart method

Second option is because it is ThreadException.

Bot above problems were discussed before in following threads:

  1. How can I set up .NET UnhandledException handling in a Windows service?
  2. Global exception handler for windows services?
Sign up to request clarification or add additional context in comments.

2 Comments

Bingo. I am putting it in the OnStart method. Thanks.
ThreadException is specific to Windows Forms and doesn't apply to Windows Services (stackoverflow.com/a/2014684/8479).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.