Performance monitoring with System.Diagnostics in .NET Framework
All the applications that we run on the server use processes and processes are at the center of our interest in understanding how our application runs. Process is a task performed by any give OS. Our application can have multiple processes behind it. Process can also be visualized as 4GB of space with the following memory slots from 0x00000000 to 0xFFFFFFF. 4GB is a standard space allocation in 32 bit machine and memory allocates whatever memory is required by the process. There is also concept of Virtual Memory when 4KB of memory saved to disk.
.NET Framework provides Process class that can access OS level process on local machine or on remote machine. Four methods can be used to determine what processes are currently running on the machine.
GetCurrentProcess – it does not take any parameters
Process MyProcess = Process.GetCurrentProcess();
Console.WriteLine("Process: " + MyProcess);
GetProcessById returns information about individual processes.
Process SpecificProcess = null;
try
{
SpecificProcess = Process.GetProcessById(2);
Console.WriteLine(SpecificProcess.ProcessName);
}
catch (ArgumentException Problem)
{
Console.WriteLine(Problem.Message);
}
GetProcessesByName returns information about multiple modules loaded by the process.
Process[] SpecificProcesses = null;
SpecificProcesses = Process.GetProcessesByName("explorer", "machinename");
foreach (Process ThisProcess in SpecificProcesses)
{
Console.WriteLine(ThisProcess.ProcessName);
}
GetProcesses method takes machine names and returns all processes being currently executed on the machine. It has several Exceptions. They are: PlatformNotSupportedException, ArgumentNullException, InvalidOperationException, Win32Exception, ArgumentException
Process[] AllMyProcesses = null;
try
{
AllMyProcesses = Process.GetProcesses("mymachinename");
foreach (Process MyCurrent in AllMyProcesses)
{
Console.WriteLine(MyCurrent.ProcessName);
}
}
catch (ArgumentException MyProblem)
{
Console.WriteLine(MyProblem.Message);
}