概述
默认的Tracking Profile会提取工作流中所有的跟踪信息,我们可以创建自己的跟踪配置文件来筛选跟踪信息。我们接着使用WF中的跟踪服务(3):使用SqlTrackingService跟踪规则中的例子,我们通过自定义tracking profile文件来跟踪RuleSet中变量A B C D的值的变化情况。
每一个TrackingProfile对象包含三个属性(WorkflowTrackPoints,ActivityTrackPoints,UserTrackPoints)表示三种跟踪数据,每一个属性都是相关的(WorkflowTrackPoint,ActivityTrackPoint,UserTrackPoint)的集合。每一个Track Point对象都有一个MatchingLocations属性表示该跟踪点匹配的位置。MatchingLocations相当于你主要的过滤器。例如一个WorkflowTrackPoint,你使用MatchingLocations来标识你要跟踪的位置如Created,Started等等。ActivityTrackPoint和UserTrackPoint除了该属性还有一个ExcludedLocations属性表示排除的位置。ActivityTrackPoint和UserTrackPoint还支持Extracts属性。表示你用来定义你要提取的独立的域或属性,还有一个Annotations属性你可以添加描述信息。WorkflowTrackingLocation获取将为此位置匹配的工作流状态事件的列表,ActivityTrackingLocation,UserTrackingLocation也是一个道理。
我们前面的例子中都是使用默认的Tracking Profile,它以一个XML串的形式存于DefaultTrackingProfile表的TrackingProfileXml字段中。我们自定义Tacking Pofile将生成一个XML串,存入TrackingProfile表的TrackingProfileXml字段中,自定义Tacking Pofile只对指定的工作流有效,默认的Tacking Pofile对所有没有自定义Tacking Pofile的工作流有效。如果所有工作流要使用同样的筛选,我们就可以直接修改默认的Tacking Pofile,所有的Tracking profile都包含一个版本Version数字。每一次你修改一个存在的Profile时,你都必须要增加Version数字,工作流引擎一直会使用版本Version数字比较高的Profile。跟踪配置文件在经过序列化后看起来将如下所示:
<?xml version="1.0" encoding="utf-16" standalone="yes"?> <TrackingProfile xmlns="http://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>
跟踪配置文件是通过使用跟踪配置文件架构创建的。
存储过程
Sql跟踪数据库中有一些存储过程用来管理Tracking profile,有两种方式来使用Tracking Profile。一种可以使用WF提供的对象模型TrackingProfile类,以xml的形式存储在数据库中。另一种可以直接使用XML文件, 但是,必须将以 XML 格式表示的跟踪配置文件反序列化为 TrackingProfile 对象,因为运行时需要 TrackingProfile 对象。下面是相关操作Tracking Profile的存储过程:
1.GetTrackingProfile:得到给定工作流类型的跟踪配置文件。
2.UpdateTrackingProfile:更新给定工作流类型的跟踪配置文件。
3.DeleteTrackingProfile:删除给定工作流类型的跟踪配置文件。该存储过程不会从 SQL 跟踪数据库中移除跟踪配置文件。它只是解除跟踪配置文件与给定工作流类型之间的关联。
4.SetInstanceTrackingProfile:设置给定工作流实例的跟踪配置文件。若要为运行中的工作流实例更新跟踪配置文件,请从要更改跟踪配置文件的 WorkflowInstance 对象中调用 SetInstanceTrackingProfile 存储过程,然后调用ReloadTrackingProfiles 方法。
5.DeleteInstanceTrackingProfile:删除给定工作流实例的跟踪配置文件。DeleteInstanceTrackingProfile 存储过程不会从 SQL 跟踪数据库中删除跟踪配置文件,而只是解除跟踪配置文件与给定工作流实例之间的关联。若要为运行中的工作流实例更新跟踪配置文件,请从要解除与跟踪配置文件关联的 WorkflowInstance 对象中调用 DeleteInstanceTrackingProfile 存储过程,然后调用 ReloadTrackingProfiles 方法。
6.GetCurrentDefaultTrackingProfile:检索 SqlTrackingService 使用的当前默认跟踪配置文件。当 SqlTrackingService 的 UseDefaultProfile 属性设置为“True”,且当工作流类型没有关联的 TrackingProfile 时,使用默认配置文件。7.GetDefaultTrackingProfile:得到默认的跟踪配置文件。
8.UpdateDefaultTrackingProfile:更新 SqlTrackingService 所使用的默认跟踪配置文件。 当 SqlTrackingService 的 UseDefaultProfile 属性设置为 true,并且工作流类型没有关联的 TrackingProfile 时,使用默认配置文件。不能删除默认跟踪配置文件。要停止使用默认配置文件,请将 SqlTrackingService 的 UseDefaultProfile 属性设置为 false。
实现TrackProfileHelper 类
我们来封装一个类来完成对TrackProfile的基本操作,TrackingProfileHelper类。主要使用[GetTrackingProfile],[UpdateTrackingProfile],[DeleteTrackingProfile]存储过程,代码如下:
namespace CarySqlRuleTracking { public class TrackingProfileHelper { private String strConn = String.Empty; public TrackingProfileHelper(String connString) { strConn = connString; } public TrackingProfile RetrieveProfile(Type workflowType) { TrackingProfile profile = null; try { String profileXml = null; using (SqlConnection conn= new SqlConnection(strConn)) { SqlCommand command = new SqlCommand("GetTrackingProfile", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter(
"@TypeFullName", workflowType.FullName)); command.Parameters.Add(new SqlParameter( "@AssemblyFullName", workflowType.Assembly.FullName)); command.Parameters.Add(new SqlParameter( "@Version", null)); command.Parameters.Add(new SqlParameter( "@CreateDefault", true)); command.Connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { profileXml = reader["TrackingProfile"] as String; } reader.Close(); } } if (profileXml != null) { TrackingProfileSerializer serializer= new TrackingProfileSerializer(); using (StringReader reader = new StringReader(profileXml)) { profile = serializer.Deserialize(reader); } } } catch (SqlException e) { Console.WriteLine("SqlException in RetrieveProfile: {0}", e.Message); throw;
} return profile; } public void UpdateProfile(TrackingProfile profile, Type workflowType) { try { String profileXml = null; TrackingProfileSerializer serializer= new TrackingProfileSerializer(); using (StringWriter writer = new StringWriter(new StringBuilder())) { serializer.Serialize(writer, profile); profileXml = writer.ToString(); } if (profileXml != null) { using (SqlConnection conn= new SqlConnection(strConn)) { SqlCommand command = new SqlCommand( "UpdateTrackingProfile", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter( "@TypeFullName", workflowType.FullName)); command.Parameters.Add(new SqlParameter( "@AssemblyFullName", workflowType.Assembly.FullName)); command.Parameters.Add(new SqlParameter( "@Version", profile.Version.ToString())); command.Parameters.Add(new SqlParameter( "@TrackingProfileXml", profileXml)); command.Connection.Open(); command.ExecuteNonQuery(); } } } catch (SqlException e) { Console.WriteLine("SqlException in UpdateProfile: {0}", e.Message); throw; } }
public void DeleteProfile(Type workflowType) { try { using (SqlConnection conn= new SqlConnection(strConn)) { SqlCommand command = new SqlCommand( @"DELETE FROM TrackingProfile WHERE WorkflowTypeId IN (SELECT TypeId FROM Type WHERE TypeFullName = @TypeFullName AND AssemblyFullName = @AssemblyFullName)",conn); command.Parameters.Add(new SqlParameter( "@TypeFullName", workflowType.FullName)); command.Parameters.Add(new SqlParameter( "@AssemblyFullName", workflowType.Assembly.FullName)); command.Connection.Open(); command.ExecuteNonQuery(); } } catch (SqlException e) { Console.WriteLine("SqlException in DeleteProfile: {0}", e.Message); throw; } } } }
自定义Tracking Profile
1.我们使用WF中的跟踪服务(3):使用SqlTrackingService跟踪规则这文中的例子,我们只需要跟踪活动的Executing和Closed状态来查看踪RuleSet中A B C D各个值在PolicyActivity执行前后的变化情况。代码如下:
private static void AddCustomTrackingProfile() { TrackingProfileHelper helper = new TrackingProfileHelper(strConn); TrackingProfile profile = helper.RetrieveProfile( typeof(CarySqlRuleTracking.CarySqlRuleTrackingWorkflow)); if (profile != null) { ActivityTrackingLocation location = new ActivityTrackingLocation(); location.ActivityTypeName = "PolicyActivity"; location.ExecutionStatusEvents.Add(ActivityExecutionStatus.Executing); ActivityTrackPoint point = new ActivityTrackPoint(); point.Extracts.Add(new WorkflowDataTrackingExtract("A")); point.Extracts.Add(new WorkflowDataTrackingExtract("B")); point.Extracts.Add(new WorkflowDataTrackingExtract("C")); point.Extracts.Add(new WorkflowDataTrackingExtract("D")); point.Annotations.Add("--RuleSet执行之前--"); point.MatchingLocations.Add(location); profile.ActivityTrackPoints.Add(point);
location = new ActivityTrackingLocation(); location.ActivityTypeName = "PolicyActivity"; location.ExecutionStatusEvents.Add(ActivityExecutionStatus.Closed); point = new ActivityTrackPoint(); point.Extracts.Add(new WorkflowDataTrackingExtract("A")); point.Extracts.Add(new WorkflowDataTrackingExtract("B")); point.Extracts.Add(new WorkflowDataTrackingExtract("C")); point.Extracts.Add(new WorkflowDataTrackingExtract("D")); point.Annotations.Add("--RuleSet执行之后--"); point.MatchingLocations.Add(location); profile.ActivityTrackPoints.Add(point); profile.Version = new Version(profile.Version.Major, profile.Version.Minor + 1, 0);
helper.UpdateProfile(profile,typeof(CarySqlRuleTracking.CarySqlRuleTrackingWorkflow)); }
}
private static void DeleteCustomTrackingProfile() { TrackingProfileHelper helper = new TrackingProfileHelper(strConn); helper.DeleteProfile(typeof(CarySqlTracking.CaryRuleWorkflow)); }
2.在WF中的跟踪服务(3):使用SqlTrackingService跟踪规则文章中例子的基础上,宿主程序中工作流实例执行前后添加如下代码:
AddCustomTrackingProfile();
DeleteCustomTrackingProfile();
3.执行结果如下图:
MSDN中有一个“跟踪配置文件设计器”示例,这个例子值得大家好好研究下,地址:ms-help://MS.MSDNQTR.v90.chs/wcf_wf_samples/html/a280199c-3000-4148-ac63-cb5aaeac07c8.htm