在坚持学习WF(22):跟踪规则这篇文章我们讲述了如何使用Trace来对规则进行跟踪,我们也可以使用SqlTrackingService跟踪规则,我们先来建立一个顺序型工作流程序,工作流设计器中我们只添加一个PolicyActivity活动,并设计他的RuleSet,我们还是使用坚持学习WF(22):跟踪规则这篇文章中的RuleSet,具体请看下表:
Rule | Conditon | ThenAction | ElseAction |
RuleC | this.D < 100 | this.B = this.B - 12 |
|
RuleB | this.B > 50 | this.C = "Preferred" |
this.C = "Normal" |
RuleA | this.A > 10 | this.B = 60 |
this.B = 40 |
工作流代码中添加相关变量如下:
private intA = 12;
private intD = 99;
private intB = 0;
private stringC = "";
宿主程序中我们加载SqlTrackingService服务,代码如下:
namespace CarySqlRuleTracking { class Program { private static String strConn = String.Format("Initial Catalog={0};Data Source={1};
Integrated Security={2};", "WorkflowTracking", @"localhost\SQLEXPRESS", "SSPI"); static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { SqlTrackingService sts = new SqlTrackingService(strConn); AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate(object sender,
WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender,
WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; workflowRuntime.AddService(sts); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(
CarySqlRuleTracking.CarySqlRuleTrackingWorkflow)); Console.WriteLine("---工作流执行开始---"); instance.Start(); waitHandle.WaitOne(); Console.WriteLine("---工作流执行结束---"); TrackingConsoleWriter trackingWriter = new TrackingConsoleWriter(strConn); trackingWriter.DisplayTrackingData(instance.InstanceId);
} } } }
这样跟踪数据就写入到跟踪数据库中了,我们还是使用WF中的跟踪服务(2):使用SqlTrackingService这篇文章中的
TrackingConsoleWriter类来将跟踪信息显示出来,代码如下:
TrackingConsoleWriter trackingWriter= new TrackingConsoleWriter(strConn);
trackingWriter.DisplayTrackingData(instance.InstanceId);
我们使用SqlTrackingQuery查询的时候。SqlTrackingWorkflowInstance有一个UserEvents属性,该属性包括UserTrackingRecord集合,每一个UserTrackingRecord都有一个UserData属性。如果这个UserTrackingRecord包含Rule数据的话,该UserData属性是一个RuleActionTrackingEvent对象的实例,该对象里包含RuleName和ConditionResult属性。当规则执行时,将发送 RuleActionTrackingEvent 对象作为用户跟踪点。在我们自己开发的TrackingConsoleWriter类中,下面的代码是处理这个的:
UserTrackingRecorduserRecord = record asUserTrackingRecord;
if(userRecord.UserData isRuleActionTrackingEvent)
{
WriteRuleData(userRecord);
}
private static void WriteRuleData(UserTrackingRecord userRecord) { RuleActionTrackingEvent ruleAction = userRecord.UserData as RuleActionTrackingEvent; Console.WriteLine("{0:HH:mm:ss.fff} RuleAction from {1} Rule:{2} Result:{3}", userRecord.EventDateTime,userRecord.QualifiedName, ruleAction.RuleName,ruleAction.ConditionResult); }
执行工作流结果如下:
上面的结果中包含RuleSet中各个规则的计算情况,下一篇文章中我们使用自定义跟踪配置文件来跟踪Rule中具体的变量的变化情况。
本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2008/11/22/RuleSqlTracking.html,如需转载请自行联系原作者