问题描述
假设我有一些要跟踪其性能信息的方法.使用 System.Diagnostics.Tracing.EventSource
,我可以看到两种实现此目的的逻辑方法:首先,可以编写两个事件,一个在开始时,一个在结束时,然后事件的使用者可以计算持续时间:
Assume I have some method that I want to trace performance information for. Using System.Diagnostics.Tracing.EventSource
, I can see two logical ways of doing this; first, one could write two events, one at the start, and one at the end, and then the consumer of the events could compute the duration:
MySource.Log.OperationStart();
RunMyOperation();
MySource.Log.OperatingFinish();
或者,我可以自己计算持续时间,只写一个事件:
Or, I can compute the duration myself, and only write a single event:
var sw = System.Diagnostics.Stopwatch.StartNew();
RunMyOperation();
sw.Stop();
MySource.Log.OperationFinish(sw.ElapsedTicks);
第二种方法的好处是不涉及数学.缺点是即使没有人在听,我也必须创建秒表,操作秒表并写出数据.同样,这是额外的一行代码,主观上也不像漂亮".
The upside to this second method is that no math is involved; the downside is that I have to create the stopwatch, manipulate it, and write out the data even when nobody is listening. It's an extra line of code, as well, and it's not as "pretty", subjectively.
哪个是首选方法?最佳做法"怎么说?
Which is the preferred method? What do the "best practices" say?
推荐答案
始终记录2个事件!将 OpCode Start 和 Stop Opcode 添加到第一个方法中,以查看哪个事件是哪个操作.
Always log 2 events! Add to the first Method the OpCode Start and the Stop Opcode to the second one to see which event is which Operation.
这篇关于使用EventSource时,我应该记录两个事件,还是计算持续时间并仅记录一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!