wyDay blog  |  Downloads  |  Buy
Support forum
wyDay blog
wyDay Home

The wyDay blog is where you find all the latest news and tips about our existing products and new products to come.

wyDay blog

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)
Debugger.Launch(); Debugger.Break();

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 Debugger.Break()
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 our blog's RSS Feed or follow Wyatt (CEO of wyDay) on Mastodon (@wyatt@hachyderm.io) to keep up-to-date with our latest posts.