Diagnostics in the C# Programming

|
| By Webner

The System. Diagnostics namespace provides a process class which has some method to run external .exe or another application into a C#.

This also enables you to debug and trace code; start, stop, and kill processes; monitor system performance; and read and write event logs.
C#

Trace Class:

Trace Class provides methods that help us isolate problems and fix them without disturbing a running system.
Example :
using System;
using System.Diagnostics;
class TestTrace
{
Public static void Main()
{
Trace.WriteLine("Entering Main");
Console.WriteLine("Welcome To Webners");
Trace.WriteLine("Exiting Main");
}
}

Debug Class:

Debug class provides the methods that help us to print debugging information and can check our logic with assertions, we can make it more robust without impacting the performance and code size of our product.

Example:
using System;
using System.Data;
using System.Diagnostics;
class DebugTest
{
static void Main()
{
Debug.WriteLine("Entering Main");
Console.WriteLine("Welcome To Webners");
Debug.WriteLine("Exiting Main");
}
}

Event Log:

EventLog provides a method that helps us to read existing logs, write entries to logs, create or delete event sources, delete logs, and respond to log entries. we can also create new logs when creating an event source.

Example:
using System;
using System.Diagnostics;
using System.Threading;
Class TestEventLog
{
Public static void Main()
{
EventLog.CreateEventSource(“NewSource", "NewLog");
// The source is created.
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, the application use the source second time.");
}
EventLog obj = new EventLog(); // Create an EventLog instance and assign its source.
obj .Source = "NewSource";
obj .WriteEntry("Writing to event log.");
}
}

Process Class:

Process Class provides methods for launching another application in the C# Programming

Let an example of the Process class to start a process :
using System.Diagnostics; //Namespace required to use process Class
namespace ProcessSample
{
class SampleProcessClass
//Let a sample class as process
{
public static void Main()
{
using (Process P1 = new Process())
{
P1.StartInfo.FileName = "C:\\MyApp.exe";
// Here , MyApp is the external execution file we are
// launching through Code
P1.Start();
}
}
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *