Performance counters are one way, and the System.Diagnostics.Stopwatch class is a good base place to look for this.
With performance counters (other than those provided), you need to manage both the event tracking infrastructure and report data. The base classes of the performance counter provide connection details for connecting to the event log, but you need to provide a different reporting infrastructure if you need to report data in another way (for example, to a log file or database).
A stopwatch is a wrapper around a high-performance timer that gives you permission to microsecond or nanosecond depending on the processor or platform. If you donβt need such a high resolution, you can use System.DateTime..Now.Ticks to get the current hour counter for the processor clock and do the differential math with this, giving you a millisecond or bet accuracy for most operations.
When tracking CPU statistics, remember that in some cases multiple processors and multiple cores will complicate any accurate statistics.
Last warning with performance counters, remember that not all performance counters are on all machines. For example, ASP.NET counters are not available on a computer on which IIS is not installed, etc.
source share