Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 733
Phil Avatar asked Jan 07 '10 04:01

Phil


People also ask

What is high performance logging?

High Performance Logging (HPL) is an initiative of the Canadian Woodlands Forum and BioApplied, aimed at providing solutions for improving operational efficiency and business performance in today's forestry operations.

What is performance logging?

The Performance Log is an excellent tool used by employees and supervisors to help document work performance throughout the year.

How to write logging in c#?

Each file is written by a separate logger, so you need to use both inside of your code: ILog Log = LogManager. GetLogger(typeof(LogTest)); ILog ErrorLog = LogManager. GetLogger("error"); Log.Info("Debug message"); ErrorLog.

How to log the Error in c#?

GetCurrentClassLogger(); // creates a logger using the class name // use it: logger.Info(...); logger. Error(...); // and also: logger. ErrorException("text", ex); // which will log the stack trace. One additional note if using error.


2 Answers

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

like image 158
TheUnknownDotNet Avatar answered Sep 22 '22 13:09

TheUnknownDotNet


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.

like image 36
GrayWizardx Avatar answered Sep 21 '22 13:09

GrayWizardx