Query Using SQLTrackingService Sample
This sample illustrates the use of the SQL tracking service that is provided by Windows Workflow Foundation to track the execution of a workflow.
The workflow runtime raises certain events while it is executing a workflow. The tracking service can catch these events and store information about them in a SQL Server database.
The workflow runtime raises the following types of events:
Workflow instance-level events. These events are raised when a workflow instance enters into a particular state. For example, an event is raised when the workflow instance is initialized or when the workflow instance is executing.
Activity-level events. A workflow consists of activities, and each activity follows a life cycle. During its life cycle, the activity passes through multiple states. An event is raised when an activity reaches a particular state.
User-tracked events. A workflow author can create custom tracking events and add them to the code-beside of the workflow.
Data from the workflow data context can be emitted and given to the tracking service together with the events.
When you are tracking a workflow, you might not be interested in all events for all states and activities. You can create a tracking profile to specify the events that you are interested in.
Note
The time value in tracking data is in Coordinated Universal Time (Greenwich Mean Time).
Creating the Custom Tracking Profile
The sample illustrates how to create a custom tracking profile. This profile is inserted into the SQL tracking database to track the specified events. The tracking profile is created by using the tracking profile object model.
The tracking profile object model provides a set of classes that help create the instance tracking point (WorkflowTrackPoint), activity tracking points (ActivityTrackPoint), and user track points (UserTrackPoint). Each tracking point also contains a set of matching locations (WorkflowTrackingLocation, ActivityTrackingLocation, and UserTrackingLocation). A matching location describes the event to be tracked (which is different based on whether it is an instance track point or an activity track point) and the activity (or workflow in case of an instance track point) to be tracked.
The data to be extracted can also be specified in the track point. In this example, no data extracts have been specified.
Use the following code to create the tracking profile:
TrackingProfile profile = new TrackingProfile();
ActivityTrackPoint trackPoint = new ActivityTrackPoint();
ActivityTrackingLocation trackingLocation = new ActivityTrackingLocation(typeof(Activity));
trackingLocation.MatchDerivedTypes = true;
foreach (ActivityExecutionStatus status in Enum.GetValues(typeof(ActivityExecutionStatus)))
{
trackingLocation.ExecutionStatusEvents.Add(status);
}
trackPoint.MatchingLocations.Add(trackingLocation);
profile.ActivityTrackPoints.Add(trackPoint);
profile.Version = new Version("3.0.0.0");
WorkflowTrackPoint wTrackPoint = new WorkflowTrackPoint();
WorkflowTrackingLocation wLocation = new WorkflowTrackingLocation();
foreach (TrackingWorkflowEvent wEvent in Enum.GetValues(typeof(TrackingWorkflowEvent)))
{
wLocation.Events.Add(wEvent);
}
wTrackPoint.MatchingLocation = wLocation;
profile.WorkflowTrackPoints.Add(wTrackPoint);
TrackingProfileSerializer serializer = new TrackingProfileSerializer();
StringWriter writer = new StringWriter(new StringBuilder(),CultureInfo.InvariantCulture);
serializer.Serialize(writer, profile);
String trackingprofile = writer.ToString();
In the previous code example, the last four lines specify how the tracking profile can be serialized into an XML format; the tracking profile is then added to the database by using the UpdateTrackingProfile stored procedure.
After the tracking profile is serialized, it looks like the following:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<TrackingProfile xmlns="https://schemas.microsoft.com/winfx/2006/workflow/trackingprofile" version="3.0.0.0">
<TrackPoints>
<WorkflowTrackPoint>
<MatchingLocation>
<WorkflowTrackingLocation>
<TrackingWorkflowEvents>
<TrackingWorkflowEvent>Created</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Completed</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Idle</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Suspended</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Resumed</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Persisted</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Unloaded</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Loaded</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Exception</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Terminated</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Aborted</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Changed</TrackingWorkflowEvent>
<TrackingWorkflowEvent>Started</TrackingWorkflowEvent>
</TrackingWorkflowEvents>
</WorkflowTrackingLocation>
</MatchingLocation>
</WorkflowTrackPoint>
<ActivityTrackPoint>
<MatchingLocations>
<ActivityTrackingLocation>
<Activity>
<Type>System.Workflow.ComponentModel.Activity, System.Workflow.ComponentModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Type>
<MatchDerivedTypes>true</MatchDerivedTypes>
</Activity>
<ExecutionStatusEvents>
<ExecutionStatus>Initialized</ExecutionStatus>
<ExecutionStatus>Executing</ExecutionStatus>
<ExecutionStatus>Canceling</ExecutionStatus>
<ExecutionStatus>Closed</ExecutionStatus>
<ExecutionStatus>Compensating</ExecutionStatus>
<ExecutionStatus>Faulting</ExecutionStatus>
</ExecutionStatusEvents>
</ActivityTrackingLocation>
</MatchingLocations>
</ActivityTrackPoint>
</TrackPoints>
</TrackingProfile>
The tracking profile is created by using the tracking profile schema.
Inserting the Tracking Profile in the SQL Tracking Database
The tracking profile is inserted into the tracking profile database by using the UpdateTrackingProfile stored procedure. The stored procedure stores the profile for a particular workflow type. When the runtime requests the tracking service for a profile for the type, the tracking service returns this custom profile instead of the default profile.
Registering the Tracking Service with the Workflow Host
Use the following code to register a tracking service with the workflow runtime:
WorkflowRuntime wRuntime = new WorkflowRuntime();
wRuntime.AddService(new SqlTrackingService(connectionString));
wRuntime.StartRuntime();
The second line registers the SQL tracking service with the workflow runtime. The connection string is the connection string of the tracking database.
Retrieving the tracking information
Once the tracking information has been saved to the database, it can be retrieved using a SqlTrackingQuery object, using the TryGetWorkflow method.
Creating the Tracking Database
The SQL services that are installed by Windows Workflow Foundation use Microsoft SQL Server to store information. You can use Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE) for these tasks.
Windows Workflow Foundation Setup does not install the databases that are required by these services; however, Setup installs the SQL scripts for creating and configuring the databases for these services.
To create the SQL tracking database
Using Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE), create a new database named Tracking by using the SQL query statement:
CREATE DATABASE Tracking
Note
When using both the SqlTrackingService and the SqlWorkflowPersistenceService in a workflow application, it is recommended to use a single database for both persistence and tracking.
In the SQL Query Analyzer workspace, select the database that you created in step 1 in the list of available databases.
On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\Tracking_Schema.sql.
Execute the query by clicking Execute or by pressing F5 to create the SQL Tracking Service tables.
On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\Tracking_Logic.sql.
Execute the query by clicking Execute or by pressing F5 to create the SQL Tracking Service stored procedures.
To build the sample
Download the sample by clicking Download Sample.
This extracts the sample project to your local hard disk.
Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.
Go to the source directory of the sample.
At the command prompt, type MSBUILD <Solution file name>.
To run the sample
- In the SDK Command Prompt window, run the .exe file in the QueryUsingSqlTrackingService\bin\debug folder (or the QueryUsingSqlTrackingService \bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.
See Also
Reference
SqlTrackingService
SqlTrackingQuery
SqlTrackingWorkflowInstance
ActivityEvents
ActivityTrackingRecord
WorkflowEvents
WorkflowTrackingRecord
Other Resources
Tracking Samples
Simple Tracking Sample
Query Using SQLTrackingService Sample
Tracking Using User Track Points Sample
EventArgs Tracking Sample
ConsoleTrackingService Sample
RuleActionTrackingEvent Sample
File Tracking Service and Query Sample
Using the Tracking Profile Object Model Sample
SQL Data Maintenance Sample
Windows Workflow Tracking Services
© 2007 Microsoft Corporation. All rights reserved.