What is the Best Way to Debug a Windows Service program in Visual Studio 2008

I am using the Microsoft Log Parser in a Windows Service. The program works in a regular web page but when I put the code in a windows service it does not work. I put Breakponts in the windows service but when I run the code the program does not stop at the breakpoint. So through out my troubleshooting I have narrowed the problem down to the Log Parser software and Linq. So either Linq or the log parser software is creating the problem. Do you guys have any idea?

13.10.2009 21:50:31

I have done this many ways in the past depending on how the program runs. I think the easiest way is done with a if #DEBUG preprocessor directive around the Debugger.Launch() that way when you've built the project optimized the Debugger.Launch() call won't be compiled into the assembly.

One way we have also done done this task is with System.Windows.Forms.MessageBox.Show("attach") which allowed us to manually attach to the debugger whenever the "attach" dialog was displayed.

The last way which I do not prefer is to change the behavior of your service based on commandline params passed in. Basically opting NOT to start the services using ServiceBase.Run whenever a particular param was fired off, but calling a class that encapsulates the behavior/main function of the service.

14.10.2009 01:23:51
Re the last point - I believe you can also check Environment.UserInteractive for this.
Marc Gravell♦ 14.10.2009 05:27:29

You need to attach your debugger directly with Windows Services. This might help you: http://msdn.microsoft.com/en-us/library/7a50syb3%28VS.80%29.aspx.

13.10.2009 21:53:48

Do you want to debug the OnStart method? If so you can use System.Diagnostics.Debugger.Launch() or System.Diagnostics.Debugger.Break() method to get a chance to attach the debugger while the on start method is running, otherwise you're always to late with attaching the debugger.

13.10.2009 22:03:00

I think fat cat's suggestion of attaching your debugger to the service process sounds right. If that still doesn't work, try using Debug.WriteLine and DebugView.

13.10.2009 22:06:12