简体   繁体   中英

Good way to do performance logging (C#)

I am trying to get some detailed performance information from an application my company is developing. Examples of information I am trying to get would be how long a network transaction takes, how much CPU/memory the application is using, how long it takes for a given method to complete, etc.

I have had some failed attempts at this in the past (like trying to measure small time periods by using DateTime.Now). On top of that I don't know much of anything about getting CPU and memory statistics. Are there any good .Net classes, libraries, or frameworks out there that would help me collect this sort of information and/or log it to a text file?

What you are looking for is Performance Counters . For .net you need this Performance Counter .

Performance counters are one way to go, and the System.Diagnostics.Stopwatch class are good foundational places to look for doing this.

With performance counters (beyond those provided) you will need to manage both the infrastructure of tracking the events, as well as reporting the data. The performance counter base classes supply the connection details for hooking up to the event log, but you will need to provide other reporting infrastructure if you need to report the data in another way (such as to a log file, or database).

The stopwatch class is a wrapper around the high performance timer, giving you microsecond or nanosecond resolution depending on the processor or the platform. If you do not need that high of resolution you can use System.DateTime..Now.Ticks to get the current tick count for the processor clock and do differential math with that, giving you millisecond or bettter precision for most operations.

When tracking CPU statistics be aware that multiple processors and multiple cores will complicate any accurate statistics in some cases.

One last caution with performance counters, be aware that not all performance counters are on all machines. For instance ASP.NET counters are not present on a machine which does not have IIS installed, etc.

for a modern opensource library to do performance metrics and monitoring consider using app-metrics.

github: https://github.com/AppMetrics/AppMetrics website https://www.app-metrics.io

A platform independent open source performance recorder build on top of aspect injector can be found here: https://gitlab.com/hectorjsmith/csharp-performance-recorder . It can be added to any C# project. Instructions on how to use can be found in the README in Gitlab.

Benefits

  • It's aspect oriented, meaning you don't have to litter your code with DateTime.Now - you just annotate the appropriate methods
  • It takes care of pretty printing the results - you can do with the what you like eg printing them to a file

Notes

  • This performance recorder is focused on the timing of methods only. It doesn't cover CPU or memory usage.

For cpu/memory use performance counters. For more specific information about specific methods (or lines of code) and specific objects, use a profiler. red-gate makes a great profiler.

http://www.red-gate.com/products/ants_performance_profiler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM