在前一篇文章http://www.cnblogs.com/carysun/archive/2011/01/23/WF4-ETW.html中我们讲到了WF4中的ETW跟踪参考者,这个是WF4给我们提供好的,我们可以看下该类的结构体系:
EtwTrackingParticipant是继承自TrackingParticipant类的,如果我们要自定义自己的跟踪参考者同样我们也是继承该类,只要重写相应的方法就可以,我们自定义一个跟踪参考者将Workflow的信息写到文件当中:
public class TxtFileTrackingParticipant:TrackingParticipant
{
string fileName = "";
protected override void Track(TrackingRecord record, TimeSpan timeout)
{
fileName = @"c:\" + record.InstanceId + ".txt";
using (StreamWriter sw = File.AppendText(fileName))
{
sw.WriteLine("----------Begin Tracking----------");
sw.WriteLine(record.ToString());
sw.WriteLine("----------End Tracking----------");
}
}
}
我们简单的设计一个工作流,然后再将该TxtFileTrackingParticipant加到工作流的扩展点中,代码如下:
class Program
{
static void Main(string[] args)
{
//ETW tracking setup
TrackingProfile trackingProfile = new TrackingProfile();
trackingProfile.Queries.Add(new WorkflowInstanceQuery
{
States = { "*"}
});
trackingProfile.Queries.Add(new ActivityStateQuery
{
States = { "*" }
});
trackingProfile.Queries.Add(new CustomTrackingQuery
{
ActivityName="*",
Name="*"
});
TxtFileTrackingParticipant txtFileTrackingParticipant = new TxtFileTrackingParticipant();
txtFileTrackingParticipant.TrackingProfile = trackingProfile;
AutoResetEvent autoResetEvent=new AutoResetEvent(false);
WorkflowApplication workflowApplication = new WorkflowApplication(new Workflow1());
workflowApplication.Completed = (arg) => { autoResetEvent.Set(); };
workflowApplication.Extensions.Add(txtFileTrackingParticipant);
workflowApplication.Run();
autoResetEvent.WaitOne();
}
}
然后我们运行工作流,工作流完成后我们会看到跟踪信息已经记录到文本文件中如下:
你可以用类似的方式将工作流的跟踪信息记录到你需要的媒介中。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
2010-01-24 WF4:Workflow Service中Correlation的基本使用