大网数据广告活动
.NET 与 C# 开发

如何:创建自定义跟踪参与者

· 52CE 我爱测

工作流跟踪用于查看工作流执行的状态。 工作流运行时发出跟踪记录,这些跟踪记录描述工作流生命周期事件、活动生命周期事件、书签摘要和故障。 这些跟踪记录供跟踪参与者使用。 Windows Workflow Foundation (WF) 包括一个标准跟踪参与者,它将作为事件跟踪 Windows (ETW) 事件的跟踪记录写入。 如果这不能满足您的需求,您还可以编写自定义跟踪参与者。 本教程步骤说明如何创建自定义跟踪参与者和跟踪配置文件,该配置文件捕获 WriteLine 活动的输出,以便将这些活动显示给用户。

工作流跟踪用于查看工作流执行的状态。 工作流运行时发出跟踪记录,这些跟踪记录描述工作流生命周期事件、活动生命周期事件、书签摘要和故障。 这些跟踪记录供跟踪参与者使用。 Windows Workflow Foundation (WF) 包括一个标准跟踪参与者,它将作为事件跟踪 Windows (ETW) 事件的跟踪记录写入。 如果这不能满足您的需求,您还可以编写自定义跟踪参与者。 本教程步骤说明如何创建自定义跟踪参与者和跟踪配置文件,该配置文件捕获 WriteLine 活动的输出,以便将这些活动显示给用户。

Note: 入门教程中的每个主题都依赖于前面的主题。 若要完成本主题,必须先完成前面的主题。 若要下载完整的版本或观看教程视频演练,请参阅Windows Workflow Foundation (WF45)-入门教程。

创建自定义跟踪参与者

  1. 右键单击NumberGuessWorkflowHost中解决方案资源管理器,然后选择添加,类。 类型StatusTrackingParticipant成名称框中,然后单击添加。

  2. 在包含其他 using(或 Imports)语句的文件的顶部添加以下 using(或 Imports)语句。

    Imports System.Activities.Tracking  
    Imports System.IO  
    
    using System.Activities.Tracking;  
    using System.IO;  
    
  3. 修改 StatusTrackingParticipant 类,使它从 TrackingParticipant 继承。

    Public Class StatusTrackingParticipant  
        Inherits TrackingParticipant  
    
    End Class  
    
    class StatusTrackingParticipant : TrackingParticipant  
    {  
    }  
    
  4. 添加以下 Track 方法重写。 有多种不同类型的跟踪记录。 我们对 WriteLine 活动的输出十分感兴趣,这些活动包含在活动跟踪记录中。 如果 TrackingRecord 为 ActivityTrackingRecord 活动的 WriteLine,则 Text 的 WriteLine 将附加到工作流 InstanceId 后面的指定文件。 在本教程中,该文件将保存到宿主应用程序的当前文件夹中。

    Protected Overrides Sub Track(record As TrackingRecord, timeout As TimeSpan)  
        Dim asr As ActivityStateRecord = TryCast(record, ActivityStateRecord)  
    
        If Not asr Is Nothing Then  
            If asr.State = ActivityStates.Executing And _  
            asr.Activity.TypeName = "System.Activities.Statements.WriteLine" Then  
    
                'Append the WriteLine output to the tracking  
                'file for this instance.  
                Using writer As StreamWriter = File.AppendText(record.InstanceId.ToString())  
                    writer.WriteLine(asr.Arguments("Text"))  
                    writer.Close()  
                End Using  
            End If  
        End If  
    End Sub  
    
    protected override void Track(TrackingRecord record, TimeSpan timeout)  
    {  
        ActivityStateRecord asr = record as ActivityStateRecord;  
    
        if (asr != null)  
        {  
            if (asr.State == ActivityStates.Executing &&  
                asr.Activity.TypeName == "System.Activities.Statements.WriteLine")  
            {  
                // Append the WriteLine output to the tracking  
                // file for this instance  
                using (StreamWriter writer = File.AppendText(record.InstanceId.ToString()))  
                {  
                    writer.WriteLine(asr.Arguments["Text"]);  
                    writer.Close();  
                }  
            }  
        }  
    }  
    

    未指定跟踪配置文件时,使用默认跟踪配置文件。 使用默认跟踪配置文件时,将为所有 ActivityStates 发出跟踪记录。 因为我们只需在 WriteLine 活动的生命周期内捕获一次文本,所以我们仅从 ActivityStates.Executing 状态捕获文本。 在中创建跟踪配置文件并注册跟踪参与者,创建一个跟踪配置文件指定仅WriteLine``ActivityStates.Executing发出跟踪记录。

创建跟踪配置文件并注册跟踪参与者

  1. 右键单击WorkflowHostForm中解决方案资源管理器,然后选择查看代码。

  2. 在包含其他 using(或 Imports)语句的文件的顶部添加以下 using(或 Imports)语句。

    Imports System.Activities.Tracking  
    
    using System.Activities.Tracking;  
    
  3. 将以下代码添加到 ConfigureWorkflowApplication,紧接在将 StringWriter 添加到工作流扩展的代码之后和工作流生命周期处理程序之前。

    'Add the custom tracking participant with a tracking profile  
    'that only emits tracking records for WriteLine activities.  
    Dim query As New ActivityStateQuery()  
    query.ActivityName = "WriteLine"  
    query.States.Add(ActivityStates.Executing)  
    query.Arguments.Add("Text")  
    
    Dim profile As New TrackingProfile()  
    profile.Queries.Add(query)  
    
    Dim stp As New StatusTrackingParticipant()  
    stp.TrackingProfile = profile  
    
    wfApp.Extensions.Add(stp)  
    
    // Add the custom tracking participant with a tracking profile  
    // that only emits tracking records for WriteLine activities.  
    StatusTrackingParticipant stp = new StatusTrackingParticipant  
    {  
        TrackingProfile = new TrackingProfile  
        {  
            Queries =   
            {  
                new ActivityStateQuery  
                {  
                    ActivityName = "WriteLine",  
                    States = { ActivityStates.Executing },  
                    Arguments = { "Text" }  
                }  
            }  
        }  
    };  
    
    wfApp.Extensions.Add(stp);  
    

    此跟踪配置文件指定仅向自定义跟踪参与者发出处于 WriteLine 状态的 Executing 活动的活动状态记录。

    在添加代码后,ConfigureWorkflowApplication 的开头将类似于以下示例。

    Private Sub ConfigureWorkflowApplication(wfApp As WorkflowApplication)  
        'Configure the persistence store.  
        wfApp.InstanceStore = store  
    
        'Add a StringWriter to the extensions. This captures the output  
        'from the WriteLine activities so we can display it in the form.  
        Dim sw As New StringWriter()  
        wfApp.Extensions.Add(sw)  
    
        'Add the custom tracking participant with a tracking profile  
        'that only emits tracking records for WriteLine activities.  
        Dim query As New ActivityStateQuery()  
        query.ActivityName = "WriteLine"  
        query.States.Add(ActivityStates.Executing)  
        query.Arguments.Add("Text")  
    
        Dim profile As New TrackingProfile()  
        profile.Queries.Add(query)  
    
        Dim stp As New StatusTrackingParticipant()  
        stp.TrackingProfile = profile  
    
        wfApp.Extensions.Add(stp)  
    
        'Workflow lifecycle handlers...  
    
    private void ConfigureWorkflowApplication(WorkflowApplication wfApp)  
    {  
        // Configure the persistence store.  
        wfApp.InstanceStore = store;  
    
        // Add a StringWriter to the extensions. This captures the output  
        // from the WriteLine activities so we can display it in the form.  
        StringWriter sw = new StringWriter();  
        wfApp.Extensions.Add(sw);  
    
        // Add the custom tracking participant with a tracking profile  
        // that only emits tracking records for WriteLine activities.  
        StatusTrackingParticipant stp = new StatusTrackingParticipant  
        {  
            TrackingProfile = new TrackingProfile  
            {  
                Queries =   
                {  
                    new ActivityStateQuery  
                    {  
                        ActivityName = "WriteLine",  
                        States = { ActivityStates.Executing },  
                        Arguments = { "Text" }  
                    }  
                }  
            }  
        };  
    
        wfApp.Extensions.Add(stp);  
    
        // Workflow lifecycle handlers...  
    

显示跟踪信息

  1. 右键单击WorkflowHostForm中解决方案资源管理器,然后选择查看代码。

  2. 在 InstanceId_SelectedIndexChanged 处理程序中,添加以下代码,紧接在清除状态窗口的代码之后。

    'If there is tracking data for this workflow, display it  
    'in the status window.  
    If File.Exists(WorkflowInstanceId.ToString()) Then  
        Dim status As String = File.ReadAllText(WorkflowInstanceId.ToString())  
        UpdateStatus(status)  
    End If  
    
    // If there is tracking data for this workflow, display it  
    // in the status window.  
    if (File.Exists(WorkflowInstanceId.ToString()))  
    {  
        string status = File.ReadAllText(WorkflowInstanceId.ToString());  
        UpdateStatus(status);  
    }  
    

    在工作流列表中选择新工作流后,将加载该工作流的跟踪记录并将其显示在状态窗口中。 以下示例是完成的 InstanceId_SelectedIndexChanged 处理程序。

    Private Sub InstanceId_SelectedIndexChanged(sender As Object, e As EventArgs) Handles InstanceId.SelectedIndexChanged  
        If InstanceId.SelectedIndex = -1 Then  
            Return  
        End If  
    
        'Clear the status window.  
        WorkflowStatus.Clear()  
    
        'If there is tracking data for this workflow, display it  
        'in the status window.  
        If File.Exists(WorkflowInstanceId.ToString()) Then  
            Dim status As String = File.ReadAllText(WorkflowInstanceId.ToString())  
            UpdateStatus(status)  
        End If  
    
        'Get the workflow version and display it.  
        'If the workflow is just starting then this info will not  
        'be available in the persistence store so do not try and retrieve it.  
        If Not WorkflowStarting Then  
            Dim instance As WorkflowApplicationInstance = _  
                WorkflowApplication.GetInstance(WorkflowInstanceId, store)  
    
            WorkflowVersion.Text = _  
                WorkflowVersionMap.GetIdentityDescription(instance.DefinitionIdentity)  
    
            'Unload the instance.  
            instance.Abandon()  
        End If  
    End Sub  
    
    private void InstanceId_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        if (InstanceId.SelectedIndex == -1)  
        {  
            return;  
        }  
    
        // Clear the status window.  
        WorkflowStatus.Clear();  
    
        // If there is tracking data for this workflow, display it  
        // in the status window.  
        if (File.Exists(WorkflowInstanceId.ToString()))  
        {  
            string status = File.ReadAllText(WorkflowInstanceId.ToString());  
            UpdateStatus(status);  
        }  
    
        // Get the workflow version and display it.  
        // If the workflow is just starting then this info will not  
        // be available in the persistence store so do not try and retrieve it.  
        if (!WorkflowStarting)  
        {  
            WorkflowApplicationInstance instance =  
                WorkflowApplication.GetInstance(this.WorkflowInstanceId, store);  
    
            WorkflowVersion.Text =  
                WorkflowVersionMap.GetIdentityDescription(instance.DefinitionIdentity);  
    
            // Unload the instance.  
            instance.Abandon();  
        }  
    }  
    

生成并运行应用程序

  1. 按 Ctrl+Shift+B 生成应用程序。

  2. 按 Ctrl+F5 启动应用程序。

  3. 选择一个范围的猜测和类型的工作流开始,然后单击新游戏。 输入在猜测猜测框,然后单击转提交该值。 可以看到,工作流的状态显示在状态窗口中。 此输出是从 WriteLine 活动捕获的。 通过选择一个从切换到不同的工作流工作流实例 Id组合框,请注意,删除当前工作流的状态。 切换回上一个工作流,可以看到其状态已恢复,与以下示例类似。

    NOTE 如果切换到启用跟踪之前启动的工作流,则不会显示状态。 但是,如果如果进行了其他猜数,则将保存其状态,因为现在启用了跟踪。

    Please enter a number between 1 and 10
    Your guess is too high.
    Please enter a number between 1 and 10
    

    NOTE 此信息对于确定随机数的范围十分有用,但它并不包含有关先前进行的猜数的任何信息。 此信息位于下一步,如何:承载多个版本的工作流的并排方案。

    记下工作流实例 ID,然后播放游戏,直到播完。

  4. 打开 Windows 资源管理器并导航到NumberGuessWorkflowHost\bin\debug文件夹 (或bin\release取决于项目设置)。 可以看到,除了项目可执行文件之外,还有包含 guid 文件名的文件。 确定与上一步中已完成工作流的工作流实例 ID 对应的工作流,然后用记事本打开它。 跟踪信息所包含的内容类似于以下内容。

    Please enter a number between 1 and 10
    Your guess is too high.
    Please enter a number between 1 and 10
    Your guess is too high.
    Please enter a number between 1 and 10
    

    除了缺少用户的猜数之外,此跟踪数据不包含有关工作流的最终猜数的信息。 这是因为,跟踪信息仅包含工作流的 WriteLine 输出,而在工作流完成后从 Completed 显示的最终消息也是这样的。 在本教程的下一步中如何:承载多个版本的工作流-并行,现有WriteLine活动被修改,以显示用户的猜数,和额外WriteLine活动添加显示最终结果。 合并这些更改后,如何:承载多个版本的工作流的并排方案演示如何同时承载多个版本的工作流。

历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。

资料来源与适用范围

资料来源
Microsoft / .NET 中文文档历史快照
适用范围
历史中文快照 2019-08-18;详见原文产品版本;文档日期 03/30/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。