System.Diagnostics Namespace
The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, performance counters, and debugging and tracing tools.
Key Types
- Classes:
Process,TraceSource,Debug,Trace,EventLog,PerformanceCounter,Stopwatch,ActivitySource - Structures:
ProcessThread,ProcessModule - Enumerations:
TraceLevel,ProcessWindowStyle,DataReceivedEventHandler - Interfaces:
IDisposable,IAsyncResult - Delegates:
DataReceivedEventHandler,OutputDataReceivedEventHandler
Example: Using Process and Debug
// Example: Get current process information and write to Debug output
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Process current = Process.GetCurrentProcess();
Debug.WriteLine($"Process ID: {current.Id}");
Debug.WriteLine($"Process Name: {current.ProcessName}");
Debug.WriteLine($"Start Time: {current.StartTime}");
Debug.WriteLine($"Total Processor Time: {current.TotalProcessorTime}");
// Start a new process (e.g., Notepad) and wait for it to exit
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "notepad.exe",
UseShellExecute = true
};
using (Process p = Process.Start(psi))
{
p.WaitForExit();
Debug.WriteLine("Notepad closed.");
}
}
}