简体   繁体   中英

Intercepting/Decorating Activities in Windows Workflow

Does Windows Workflow Foundation offer a way to intercept or decorate activities for purposes such as logging? For example to create logs for every activity entry and exit point (ideally including the activity name) without modifying all the existing project code.

For example, I have a workflow with a single activity that prints "Hello world". Without making modification to the XAML file I would like to capture the entry and exit of the activity. I would like to print "Entering Hello World Activity" before entering the activity and after the activity has printed "Hello World", I would like to print "Completed Hello World Activity".

Does Windows Workflow offer a mechanism for hooking into entry and exit of an activity?


@Richard210363 has already added to the comments that this feature is supported by Windows Workflow Foundation - please can the users who chose to close this question consider reversing their decision as the question clearly has a very specific answer using the framework in question?

I wrote the following code which is able to track all the workflows and the activities.

public class ActivityTracker : TrackingParticipant
{
    protected override void Track(TrackingRecord record, TimeSpan timeout)
    {            
        if (record != null)
        {
            if (record is WorkflowInstanceRecord)
            {
                WorkflowInstanceRecord instanceRecord = record as WorkflowInstanceRecord;
                Console.WriteLine("Workflow Record: Instance: {0} - State: {1} - Definition Identity: {2}", instanceRecord.ActivityDefinitionId, instanceRecord.State, instanceRecord.WorkflowDefinitionIdentity);
            }
            else if (record is ActivityStateRecord)
            {
                ActivityStateRecord instanceRecord = record as ActivityStateRecord;
                Console.WriteLine("Activity Record: Name: {0} - State: {1}", instanceRecord.Activity.Name, instanceRecord.State);
            }
        }            
    }
}

Have a look at the workflow TrackingParticipant class.

It acts across all Activities in the workflow similar to AOP in scope.

It emits information about the entry and exit of activities.

Create a class that inherits from TrackingParticipant and override the Track method:

protected override void Track(TrackingRecord record, TimeSpan timeout)
    {
        ActivityStateRecord activityStateRecord = record as ActivityStateRecord;
        string CurrentActivityName = activityStateRecord.Activity.Name,
    }

Then attach your tracking class to the workflow before it runs.

_workflowApplication.Extensions.Add(_yourWorkFlowTrackingClass);
_workflowApplication.Run();

You can also cast a TrackingRecord to a WorkflowInstanceRecord. Between them, ActivityStateRecord and WorkflowInstanceRecord supply a lot of info about the workflow and its activities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM