views:

92

answers:

2

I have inherited a large and complex C# windows service project that crashes every now and then. The logging system is not logging any messages which I initially thought strange but I now understand that logging might fail if there's a stack overflow or out-of-memory exception.

So one of the tasks that I have is to try and find any recursive functions that might blow the stack. Is there any tooling in VS2010 or other code analysis software that would help detect recursive code?

As a second question: What else could cause logging to fail in a windows service?

(Project uses VS2010 but still targets .net 3.5 with C# 3.0)

+1  A: 

Are you attaching to the AppDomain.UnHandledException event? It should raise an event if an unhandled exception occurs. Also, have you checked the Eventlog?

It's very difficult to try and guess what could cause your service to crash. If you are attached to the event I mentioned then I guess it could only really be one of a few events, a StackOverflow exception being one. If you're not attaching to that event it could be anything.

If you're really at a loss you can always try to run the service as a console application from within Visual Studio. Visual Studio should then show you the error if it does occur. This is not always possible depending on your environment.

Jaco Pretorius
+3  A: 

Download Debug Diagnostic Tool, point it to your service and add stack overflow in the exception lists and let it run. When the service fails it will dump the memory. Open the dump in Visual Studio and check all stacks on all threads to identify the offensive code. You might need the original debugging symbols for your service to get intelligible inforamtion.

More about memory dumps debugging with VS2010 here. More about debugging this kind of problems with Tess Ferrandez watch this

Update: Tutorial on a stack overflow exception with details. It is based on a web app in IIS but you can easily apply the same technique to a service, it is just the way you take the memory dump that is different.

HTH

Cosmin Onea