bboyle1234
bboyle1234

Reputation: 5009

Best way to find out what error terminated a c# windows service?

My c# windows service is dying due to an unknown error deep down in one of the worker threads that it creates.

Is there any way to find out the exception that causes the service to be terminated?

(Without writing in a dozen try / catch / log the error codes).

Just a simple one-time report on the error that caused the service to shut down.

Upvotes: 1

Views: 375

Answers (4)

Hans Passant
Hans Passant

Reputation: 941277

It is simple, implement an event handler for AppDomain.CurrentDomain.UnhandledException and log the value of e.ExceptionObject.ToString().

Upvotes: 2

Andrew Collins
Andrew Collins

Reputation: 2681

The easy way is transferring the errors from VS to windows error log and checking there. You have to use some third party tools if you prefer to get error log list as a report instead. Logger in c#

if you prefer go for a simpler/cheaper one try the above code. Its is very easy and simpler than u thought

Upvotes: 0

Scott
Scott

Reputation: 17247

There's a couple of things that you can do. First, check the Windows error logs if you haven't already - I know this is probably obvious, and they can be a bit light on information, but it's always worth a look, because occasionally it will help.

The second approach is just a little bit more involved, and I know you've asked for something really quick and one-time, but to be honest I'm about to recommend the best way to develop Windows services, and once you've done it, you will never go back... and it will save you hours of pain, so I'll give you the advice anyway:

Extract the core of your service to be run independently in a simple console app host. This approach means that you can fully run and debug it in Visual Studio running as a plain old executable, or even on your test server with a remote debugging session attached. For the real "live" windows service, your service code is a thin wrapper around your testable, debuggable service core. This has worked for me time and time again.

Your core service will expose Start() and Stop() methods that can be called by your Windows Service host in production. That's it.

There's a really good OSS project called Topshelf that provides a full-featured and well-tested version of the wrapper I've described and you can read a developer's example of it in use it here.

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 54999

I'd say that it might be useful to add the dozen try catch so that the next time something goes wrong, you'll have a log of the error (especially useful if you later find an issue that only occurs sometimes in unclear circumstances so difficult to reproduce while debugging).

However, other than that, you can attach Visual Studio to a running process as shown in this MSDN article. To avoid having to do that I usually add some code to the startup of the service so that if the exe is started with a -c command line parameter, it'll run as a normal process which can be started from Visual Studio. This Code Project article shows something similar to this.

Upvotes: 0

Related Questions