Debugging Windows Services with Prefix

By: mwatson
  |  August 17, 2023
Debugging Windows Services with Prefix

Prefix enables developers to easily see what their code is doing as they write and test their code, including SQL queries, HTTP calls, errors, logs, and much more. Prefix is most often used with web applications, but .NET Windows Services are also supported. This article discusses how to use Prefix with a Windows Service or console application.

Learn more about Prefix

Getting Started

First thing you need to do is install Prefix. Support for non web applications is now included in Prefix.

By default Prefix only profiles web applications. So you need to tell Prefix to profile your application when it starts. There are a couple ways to do this including a command line switch of “/EnableStackify” or by creating a small ProfileProcess.txt file.

For Prefix, I suggest the command line arguments way since you are most likely running your app from Visual Studio. In the project properties add the command line argument and you should be good to go!

vs-enablestackify

Types of Windows Services/Console Apps

These types of apps can be used to do a wide variety of things. We will attempt to cover some of the most popular uses and how Prefix can be valuable for those. Depending on your type of application, different configuration steps will be required. We will walk through three common scenarios:

  • Timer based events
  • Job scheduler, like Quartz.net
  • Queue processing

We have a sample project on GitHub that shows how to do all of these.

Note: You can also enable Prefix for “self hosted web apps”. Review: Self hosted WCF or Self hosted Web API for more details on how to do this.

How it works – Tracking custom code operations

For non web applications, there is no real defined scope of what an “operation” is. Prefix and Retrace are designed to track short operations (< 60 seconds) that happen often. If you are trying to track a segment of code that never ends, it won’t work with Prefix. Tracking custom operations is done by using the ProfilerTracer object within StackifyLib. This creates a defined operation from start to finish that can be named and tracked as a single operation.

Example: Timer based events

It is common for service apps to do things on a scheduled interval like polling a database, file, website, etc. To make this show up in Prefix (or Retrace), all you need to do is define the scope of the operation.

Example code with operation defined:

How it looks in Prefix

List of the timer based transactions happening every few seconds

download-page-list

Captured trace showing a debug statement and an HTTP call

timer-download-page

See sample project for a more complete demo: TimerExample

Example: Quartz.net Job Scheduler

If you have ever managed a bunch of Windows Scheduled Tasks, you know how much of a nightmare they are. Using an open source job scheduler like Quartz can eliminate those, while at the same time allowing you to run the jobs across multiple computers. In this example we will show you how to track Quartz jobs with Prefix (and Retrace).

We suggest making all of your jobs inherit from a base job class that you create. That makes it easy to have a single place in your code to track the scope of the jobs, do logging, or other management operations.

How it looks in Prefix

List of quartz operations occurring

quartz-list

Detail view of a quartz job that just does a HTTP call as a simple example

quartz-detail

See sample project for a more complete demo: QuartzExample

Example: Processing queued messages

A common application design is using queues to increase application resilience, scalability, etc. Monitoring applications that handle queued messages can be a mystery. With Prefix (and Retrace) you can track each of these as a transaction to have visibility in to what is going on.

For this example we are showing how to track an Azure Service Bus message pump.

How it looks in Prefix

List of captured operations from processing queue messages

queue-list

Example of detailed trace of processing the queued message. In this example we aren’t doing anything except a couple log statements.

queue-detail

See sample project for a more complete demo: QueueProcessor

Other Scenarios

Windows services can be used for a very wide variety of purposes beyond the three scenarios described above. As long as you can define a scope of work that makes sense to be a repeatable operation, you should be able to make Prefix track the individual operations of your code.

 

Get Prefix Power on a server!
Get Prefix Power on a server!

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]