Monday, November 5, 2012

Workflow Custom Tracking

Today's topic is about how to enable custom tracking in Workflow? If you missed the previous post about how to enable tracking in Workflow, you have to read it first because it is related to this post.

When do we use custom tracking?
Imagine that you wish to track down all the data which are passed through a workflow activity. Or, you wish to track application exception and you want to hunt down the data that cause the exception.

How?
In your workflow code activity, use the CustomTrackingRecord.
Example:

    [Serializable]
    public sealed class ApplyLeaveActivity : CodeActivity
    {
        // Argument declaration.
        public OutArgument ApplyLeaveResult { get; set; }
        public InArgument Leave { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            // Variable declarations (if any).
            Leave applyLeaveResult = default(Leave);

            // Argument initialization (if any).
            Leave leave = context.GetValue(this.Leave);

            // Instantiate business components.
            LeaveComponent leaveComponent = new LeaveComponent();

            // Call business component methods.

            try
            {
                applyLeaveResult = leaveComponent.ApplyLeave(leave);
            }
            catch (Exception ex)
            {
                CustomTrackingRecord customRecord = new CustomTrackingRecord("ApplyLeave", TraceLevel.Error)
                {
                    Data =
                    {
                        {"InstanceId", context.WorkflowInstanceId},
                        {"ApplyBy", leave.ApplyBy},
                        {"ApplyDate", leave.ApprovalDate},
                    }
                };

                context.Track(customRecord);
            }

            // Set value to Out arguments (if any).
            context.SetValue(this.ApplyLeaveResult, applyLeaveResult);
        }

    }

Note: you can put whatever value into the Data property of CustomTrackingRecord. The custom tracking record is not only meant for exception tracking, it can be used for any other purpose.

Now, add custom tracking query to the tracking profile in web.config.

<tracking>
     <profiles>
       <trackingProfile name="TrackEverything" implementationVisibility="All">
         <customTrackingQueries>
            <customTrackingQuery activityName="*">
            </customTrackingQuery>
          </customTrackingQueries>
        </trackingProfile>
     </profiles>
</tracking>



Finally, try out your workflow. And, following is the screenshot of how the custom tracking record look like in event viewer.




The value in the Data property of CustomTrackingRecord is displayed in the event viewer and it is how it look like base on the sample code above:

Data=<items><item name="InstanceId" type="System.Guid">b5d8cffd-8a4e-4943-bae1-bead0bed9e63</item><item name="ApplyBy" type="System.Int32">0</item><item name="ApplyDate" type="System.DateTime">0001-01-01T00:00:00Z</item>

If you wish to have a look at my implementation, feel free to download it from HERE.

Next topic, I will share about SQL tracking.

No comments:

Post a Comment

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...