Tracing in the Policy Activity

The PolicyActivity is rather cool but with more than a few rules in the RuleSet and full chaining enabled it can become a bit hard to see what is going on. Now it would be nice if we could step through the rules with the debugger but if there is a way I haven’t found it yet. Of course you could add Console.WriteLine statements to the actions, after all they can be multiple statements. There is however an easier way that will show exactly what the runtime engine is doing and that is tracing.

 

Add the following to the app.config:

  <system.diagnostics>
    <switches>
      <add name=”System.Workflow.Activities.Rules” value=”Verbose” />
    </switches>
  </system.diagnostics>
And run the workflow again. This time the Immediate Window will contain a trace with all rules being loaded and their dependencies. After that the trace will contain all evaluations of conditions and results thereof. The following is an example of a small RuleSet I created:

 

System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "+5%" Condition dependency: "this/amount/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "+5%" THEN side-effect: "this/rate/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "10%" Condition dependency: "this/age/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "10%" THEN side-effect: "this/rate/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "50%" Condition dependency: "this/age/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "50%" THEN side-effect: "this/rate/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" Condition dependency: "this/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" Condition dependency: "this/amount/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" Condition dependency: "this/rate/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" Condition dependency: "this/discount/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" THEN side-effect: "this/discount/"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "+5%" THEN actions trigger rule "Calc"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "10%" THEN actions trigger rule "Calc"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "50%" THEN actions trigger rule "Calc"
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" THEN actions trigger rule "Calc"
System.Workflow.Activities.Rules Information: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Executing
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating condition on rule "+5%".
System.Workflow.Activities.Rules Information: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "+5%" condition evaluated to True.
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating THEN actions for rule "+5%".
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating condition on rule "10%".
System.Workflow.Activities.Rules Information: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "10%" condition evaluated to True.
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating THEN actions for rule "10%".
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating condition on rule "50%".
System.Workflow.Activities.Rules Information: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "50%" condition evaluated to True.
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating THEN actions for rule "50%".
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating condition on rule "Calc".
System.Workflow.Activities.Rules Information: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" condition evaluated to True.
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating THEN actions for rule "Calc".
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" side effects enable rule "Calc" reevaluation.
System.Workflow.Activities.Rules Verbose: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Evaluating condition on rule "Calc".
System.Workflow.Activities.Rules Information: 0 : Rule Set "Rule Set1": InstanceId 4c453688-cb02-41e9-a641-39921711d2a0: Rule "Calc" condition evaluated to False.

Rather nice as you can see exactly what is going on. Just make sure you name your rules well otherwise it will still be somewhat hard to follow.
Enjoy!

Maurice de Beijer
 

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>