bright, fresh software
Downloads  |  Buy

Wyatt Says...

May 6th, 2012

Windows ServiceOver the last couple of months I’ve gotten a surprising number of questions asking about the same thing: how to debug a Windows Service like you would debug a normal Window Forms application. There are lots of hacks to pseudo-debug a Windows service like running your code from a console, or even adding a massive amount of logging throughout your code.

But what if you need to run your Windows Service as it normally runs (i.e. as an actual service) in order to track down bugs?

The good news is that’s there’s a little snippet of code to make debugging Windows Services a snap. The first thing to do is to add the following DebugMode() function to your app:


/// <summary>Helper function to attach a debugger to the running service.</summary>
static void DebugMode()
    if (!Debugger.IsAttached)


Visual Basic .NET

''' <summary>Helper function to attach a debugger to the running service.</summary>
Shared Sub DebugMode()
    If Not Debugger.IsAttached Then
    End If

End Sub

When you call the newly added DebugMode() function within your Windows Service, if there isn’t already a debugger attached to your service it gives you the option of adding a debugger:

Moneybookers a.k.a. Skrill

Now you can debug your service like you would a Windows Forms application. Also, because you’re using the [Conditional("DEBUG")] attribute on the DebugMode() function when you compile your service in “Release” mode all the calls to the “DebugMode()” function will be removed.

Pretty cool, huh?

Subscribe to Wyatt Says...

Subscribe to the 'Wyatt Says...' RSS Feed and keep up to date on on my articles on updaters, usability, open source C# components, and software licensing.


Leave a Reply