Skip to main content

Performance Monitoring in .Net


Namespace: System.Diagnostics

Process
A Process component provides access to a process that is running on a computer. A process, in the simplest terms, is a running application. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the code of the process, including parts currently being executed by another thread.

The Process component is a useful tool for starting, stopping, controlling, and monitoring applications. Using the Process component, you can obtain a list of the processes that are running, or you can start a new process. A Process component is used to access system processes. After a Process component has been initialized, it can be used to obtain information about the running process. Such information includes the set of threads, the loaded modules (.dll and .exe files), and performance information such as the amount of memory the process is using.

Process Properties:
Process.GetProcessByID
Process.GetProcessByName
Process.GetProcesses

Performance Counter
It represents a Windows NT performance counter component.

Methods
NextSample       Obtains a counter sample, and returns the raw, or uncalculated, value for it.
NextValue          Obtains a counter sample and returns the calculated value for it.

CounterSampleCalculator.ComputeCounterValue(MyPerformanceCounter.NextSample(),MyPerformanceCounter.NextSample());

CounterCreationData
CounterCreationData is helpful in a scenario where we want to setup our own counter and PerformanceCounter Categories.

CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
                                               
// Add the counter.
CounterCreationData averageCount64 = new CounterCreationData();
averageCount64.CounterType = PerformanceCounterType.AverageCount64;
averageCount64.CounterName = "AverageCounter64Sample";
CCDC.Add(averageCount64);

// Create the category.
PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
"Demonstrates usage of the AverageCounter64 performance counter type.",CCDC);
               
Properties of CounterCreationData           
CounterHelp
CounterName
CounterType

PerformanceCounterCategory
Main Methods include Create, Delete, Exists, GetCategories and ReadCategory.
GetCategories gives a list of categories on a machine.
ReadCategory will read all the counter and performance object instance data that is associated with this category.

Some Important Performance Counters
PerformanceCounter pc = new PerformanceCounter(“ASP.NET”,”Applications Running”);

PerformanceCounter pc = new PerformanceCounter(“.NET CLR Memory”,”# Bytes in all Heaps”);
Pc.InstanceName = “_Global_”;

PerformanceCounter pc = new PerformanceCounter(“.NET CLR Exceptions”,”# of Exceps Thrown”);
Pc.InstanceName = “_Global_”;



Starting a Process

ProcessStartInfo info = new ProcessStartInfo();
Info.FileName = “iexplorer.exe”;
Process.Start(info);

Main ProcessStartInfo Properties
FileName                        executable path or system executable name;
Arguments                     String specifying multiple arguments with spaces.
UseShellExecute            Set to false if requiring Password from user.
UserName                      Set to Enviorment.UserName if required authentication.
Password                        Set to SecureString obtained from user if required authentication.
Domain                           Set to domain that user selects against which we have to authenticate.

StackFrame
A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread. The stack frame always includes MethodBase information, and optionally includes file name, line number, and column number information.

Constructors
Name
Description
StackFrame ()
Initializes a new StackFrame object.
StackFrame (Boolean)
Initializes a new StackFrame object, optionally capturing source information.
StackFrame (Int32)
Initializes a new StackFrame object that corresponds to a frame above the current stack frame.
StackFrame (Int32, Boolean)
Initializes a new StackFrame object that corresponds to a frame above the current stack frame, optionally capturing source information.
StackFrame (String, Int32)
Initializes a new StackFrame object that only contains the given file name and line number.
StackFrame (String, Int32, Int32)
Initializes a new StackFrame object that only contains the given file name, line number, and column number.

Important Methods
GetFileColumnNumber
Gets the column number in the file that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.
GetFileLineNumber
Gets the line number in the file that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.
GetFileName
Gets the file name that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.
GetMethod
Gets the method in which the frame is executing.

StackTrace
Represents a stack trace, which is an ordered collection of one or more stack frames. StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

StackTrace st = new StackTrace(true);
for(int i =0; i< st.FrameCount; i++ )
{
  // Note that high up the call stack, there is only
  // one stack frame.
  StackFrame sf = st.GetFrame(i);
  Console.WriteLine();
  Console.WriteLine("High up the call stack, Method: {0}", sf.GetMethod());

  Console.WriteLine("High up the call stack, Line Number: {0}",
  sf.GetFileLineNumber());
}

Methods:
GetFrame
Gets the specified stack frame.
GetFrames
Returns a copy of all stack frames in the current stack trace.

Comments

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Text Encoding and Decoding using .Net

Namespace: System.Text To read a file, an application must write information about the encoding in which the file was written. So, the application that creates a file must write encoding information, otherwise viewer will see block and question marks in file. Unicode UTF-32 encoding : Represents a character as sequence of four bytes (32 bits). UTF32Encoding class can be used in that perspective. Unicode UTF-16 encoding : Represents a character in 16 bits. UnicodeEncoding class can be used to convert to and from UTF-16 encoding. Unicode UTF-8 encoding : Unicode UTF-8 uses 8-bit, 16-bit, 24-bit, and up to 48-bit encoding. Values 0 through 127 use 8-bit encoding and exactly match ASCII values, 128 through 2047 use 16-bit encoding, and values from 2048 and 65535 uses 24-bit encoding. UTF8Encoding class can be used to convert to and from UTF-8 encoding. ASCII encoding : ASCII encoding encodes Latin alphabets as single 7-Bit ASCII character. It is not well su...

Using ADO.NET Transaction Object, Saving CLR Objects and SqlNotificationRequest

Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : A transaction can’t span multiple connections.   Local and distributed transaction ares supported in 1.x you have to Enterpeise seveices library to regiser multiple connections and then call EnlistDistributedTransaction. Distributed Transaction Coordinator is required for Distributed transaction that’s available on windows 2000 & +. In 2.X and later use TransactionScope object. Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : Dispoase of the transaction must be called to complete the transaction. Distributed Transaction : ...