WF4.0 基础篇 (二十二) Tracking

 

Tracking 介绍

WF跟踪是用来记录工作流执行情况

WF跟踪由以下几部分组成:

跟踪记录

Tracking Records

从工作流运行时发出

跟踪配置

Tracking Profile

用来筛选从工作流实例中发出的跟踪记录

跟踪参与者

Tracking Participants

用来订阅跟踪记录。跟踪参与者包括处理来自跟踪记录的负载的逻辑

ActivityInfo

System.Activities.Tracking.ActivityInfo

TrackingProvider

System.Activities.Tracking.TrackingProvider

 

跟踪记录 TrackingRecord

跟踪记录[TrackingRecord]

DLL:System.Activities.dll

类名:System.Activities.Tracking.TrackingRecord

 

TrackingRecord为[Abstract],WF4自带了以下功能[TrackingRecord]

 

CustomTrackingRecord

可在Activity中使用[context.Track(CustomTrackingRecord)]方式添加自定义跟踪记录

  
  
  
  
  
  
  

 

 

跟踪配置 TrackingProfile

跟踪配置[TrackingProfile]

DLL:System.Activities.dll

类名:System.Activities.Tracking.TrackingProfile

 

Queries类型为[System.Collections.ObjectModel.Collection<System.Activities.Tracking.TrackingQuery>],,WF4自带了以下功能[TrackingQuery]

 

工作流追踪包括两个主要组件:追踪参与和追踪配置。追踪配置定义了您希望运行时需要追踪的事件和数据,配置(Profiles)包括以下三种重要的查询类型:

 

 

 

 

 

ActivityStateQuery

用于指定活动的状态(如关闭)、提取数据的变量和参数

 

[States属性]:由System.Activities.Tracking.ActivityStates的[Fieldes]提供

WorkflowInstanceQuery

用于指定工作流事件

 

[States属性]:由System.Activities.Tracking.WorkflowInstanceStates的[Fieldes]提供

CustomTrackingQuery

用于指定对追踪数据的明确调用,常用语自定义的活动中

 

 

 

 

 

跟踪参与者TrackingParticipant

跟踪参与者 [TrackingParticipant]

DLL:System.Activities.dll

类名:System.Activities.Tracking.TrackingProfile

abstract

要自定义[跟踪参与者]要从该类继承

 

 

 

自定义跟踪参与者

例子下载:

https://files.cnblogs.com/foundation/CustomTrackingSample.rar

自定义跟踪参与者

public class myTrackingParticipant : System.Activities.Tracking.TrackingParticipant

{

private const String participantName = "wxwinter_myTrackingParticipant";

 

public myTrackingParticipant()

{

Console.WriteLine( "{0} Created", participantName);

}

 

 

protected override void Track(System.Activities.Tracking.TrackingRecord record, TimeSpan timeout)

{

Console.Write( "{0} emitted trackRecord: {1} Level: {2}, RecordNumber: {3}", participantName, record.GetType().FullName, record.Level, record.RecordNumber);

 

System.Activities.Tracking.WorkflowInstanceRecord workflowInstanceRecord = record as System.Activities.Tracking.WorkflowInstanceRecord;

if (workflowInstanceRecord != null)

{

Console.WriteLine( " Workflow InstanceID: {0} Workflow instance state: {1}", record.InstanceId, workflowInstanceRecord.State);

}

 

System.Activities.Tracking.ActivityStateRecord activityStateRecord = record as System.Activities.Tracking.ActivityStateRecord;

if (activityStateRecord != null)

{

IDictionary<String, object> variables = activityStateRecord.Variables;

 

StringBuilder vars = new StringBuilder();

 

if (variables.Count > 0)

{

vars.AppendLine("\n\tVariables:");

foreach (KeyValuePair<string, object> variable in variables)

{

vars.AppendLine(String.Format( "\t\tName: {0} Value: {1}", variable.Key, variable.Value));

}

}

Console.WriteLine( " :Activity DisplayName: {0} :ActivityInstanceState: {1} {2}", activityStateRecord.Activity.Name, activityStateRecord.State, ((variables.Count > 0) ? vars.ToString() : String.Empty));

}

 

System.Activities.Tracking.CustomTrackingRecord customTrackingRecord = record as System.Activities.Tracking.CustomTrackingRecord;

if ((customTrackingRecord != null) && (customTrackingRecord.Data.Count > 0))

{

Console.WriteLine("\n\tUser Data:");

foreach (string data in customTrackingRecord.Data.Keys)

{

Console.WriteLine(" \t\t {0} : {1}", data, customTrackingRecord.Data[data]);

}

}

Console.WriteLine();

 

}

}

 

定义Profile

public class myTrackingTools

{

public static System.Activities.Tracking.TrackingParticipant getTracking()

{

//(1) TrackingProfile

System.Activities.Tracking.TrackingProfile trackingProfile = new System.Activities.Tracking.TrackingProfile();

trackingProfile.Name = "wxwinterTrackingProfile";

 

//(1.1)

System.Activities.Tracking.CustomTrackingQuery customQuery = new System.Activities.Tracking.CustomTrackingQuery();

customQuery.Name = "*";

customQuery.ActivityName = "*";

 

//(1.2)

System.Activities.Tracking.WorkflowInstanceQuery instanceQuery = new System.Activities.Tracking.WorkflowInstanceQuery();

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Aborted);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Canceled);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Completed);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Idle);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Persisted);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Resumed);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Started);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Suspended);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Terminated);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.UnhandledException);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Unloaded);

instanceQuery.States.Add(System.Activities.Tracking.WorkflowInstanceStates.Unsuspended);

 

//(1.3)

System.Activities.Tracking.ActivityStateQuery activityQuery = new System.Activities.Tracking.ActivityStateQuery();

activityQuery.ActivityName = "*";

activityQuery.States.Add("*"); // System.Activities.Tracking.ActivityStates.Executing // System.Activities.Tracking.ActivityStates.Canceled

 

activityQuery.Variables.Add("*");

activityQuery. Arguments.Add("*");

 

//(1.4)

trackingProfile.Queries.Add(customQuery);

trackingProfile.Queries.Add(instanceQuery);

trackingProfile.Queries.Add(activityQuery);

 

 

//(2)

myTrackingParticipant myTracking = new myTrackingParticipant();

myTracking.TrackingProfile = trackingProfile;

 

return myTracking;

}

}

 

具有CustomTrackingRecord功能的Activity

public sealed class myActivity : CodeActivity

{

protected override void Execute(CodeActivityContext context)

{

Console.WriteLine("myActivity Execute");

 

System.Activities.Tracking.CustomTrackingRecord myRecord = new System.Activities.Tracking.CustomTrackingRecord("myActivity_CustomTrackingRecord");

 

myRecord.Data.Add("wxd", 12345);

 

myRecord.Data.Add("wxwinter", "lzm");

 

context.Track(myRecord);

 

}

}

 

 

使用

工作流

宿主

class Program

{

static void Main(string[] args)

{

System.Console.WindowWidth = 150;

 

//-----------------------------------------------------------------------------------

//WorkflowApplication

WorkflowApplication instance = new WorkflowApplication(new testWorkflow());

 

instance.Extensions.Add(myTrackingTools.getTracking());

 

instance.Run();

 

//------------------------------------- or ------------------------------------------

//WorkflowInvoke

// WorkflowInvoker invoker = new WorkflowInvoker(new testWorkflow());

 

// invoker.Extensions.Add(myTrackingTools.getTracking());

 

// invoker.Invoke();

//---------------------------------------------------------------------------------

 

System.Console.Read();

}

 

}

结果

 

 

EtwTrackingParticipant

 

[EtwTrackingParticipant]:(ETW:Enterprise Trace for Windows)

DLL:System.Activities.dll

类名:ystem.Activities.Tracking.EtwTrackingParticipant

ETW是Windows里本地组件的一个追踪系统,由操作系统中的包括驱动程序、其它核心级代码等很多组件和服务调用。写入到ETW的数据可以通过定制代码或者诸如即将推出的Windows服务AppFabric等工具来使用。AppFabric将同ETW一起搜集ETW数据并将它存储到SQL数据库。

 

posted @ 2010-02-05 14:33  WXWinter(冬)  阅读(6474)  评论(1编辑  收藏  举报