Tracking 介紹
WF跟蹤是用來記錄工作流執行情況
WF跟蹤由以下幾部分組成:
跟蹤記錄跟蹤記錄 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
自定義跟蹤參與者 自定義跟蹤參與者
public class myTrackingParticipant : System.Activities.Tracking.TrackingParticipant
定義Profile
{
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();
}
} 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數據庫 。