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.
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!
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.
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
Captured trace showing a debug statement and an HTTP call
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
Detail view of a quartz job that just does a HTTP call as a simple example
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
Example of detailed trace of processing the queued message. In this example we aren’t doing anything except a couple log statements.
See sample project for a more complete demo: QueueProcessor
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.
- IIS Error Logs and Other Ways to Find ASP.Net Failed Requests - November 6, 2019
- List of .Net Profilers: 3 Different Types and Why You Need All of Them - October 30, 2019
- View SQL Queries From Your Code With Prefix - October 22, 2019
- Log4net for .NET Logging: The Only Tutorial and 14 Tips You Need to Know - October 9, 2019
- How to Troubleshoot IIS Worker Process (w3wp) High CPU Usage - September 26, 2019