CONSOLE LOG IN C#

Introduction:

The term "console log" in C# refers to the practise of writing messages, data, or information to the console window while a console programme is running. The console window is a text-based interface for interacting with a programme by inputting commands and examining text output.

The Control center class in C# has techniques for communicating with the control center window.

In C#, the Control center class has techniques for managing the control center window. One of the critical elements of Control center is to log data, which is fundamental in programming advancement for troubleshooting, checking, and conveying input to clients.

Here's a quick glossary of concepts related to console logging in C#:

  1. Console is a static class supplied by the System namespace in C#. It represents the console window and allows you to read and write text.
  2. Logging is the method involved with recording data (log messages) in regards to a program's activity. Troubleshooting, checking, and dissecting the way of behaving of a program is a critical practice in programming improvement.
  3. Console Log: This phrase refers to the act of displaying log messages on the console window. These messages may contain information, warnings, errors, or any other data pertinent to the program's operation.
  4. A console application in C# is a programme that communicates with the user via a console window. It takes input from the console and outputs to it.
  5. Console. WriteLine() is a Control center class capability that sends a provided string to the control center window, trailed by a line eliminator (generally a newline character).
  6. String Insertion: String addition in C# permits you to embed articulations or factors into string literals straightforwardly.
  7. Console Output: The data presented in the console window as a result of running a programme. Messages, data, prompts, and any other textual information created by the programme are examples of this.
  8. Error logging entails precisely logging error messages or information pertaining to extraordinary situations or difficulties that arise during programme execution.

Generally speaking, console logging is a fundamental instrument in an engineer's tool compartment, particularly for making and troubleshooting terminal applications. It helps developers in grasping the progression of their projects, distinguishing issues, and working on the general quality and reliability of their product.

Making Use of the Console

The Console is the most simple approach to log information on the console. WriteLine is a method. As a contention, this capability yields a string to the control center, trailed by a newline character.

Console.WriteLine("This is a log message.");

String Formatting

To incorporate variables or values in your log messages, you can use string interpolation or composite formatting:

string name = "John";


int age = 30;


Console.WriteLine($"Name: {name}, Age: {age}");


// Output: "Name: John, Age: 30"

Changing the Colour of Text

You may also use Console to alter the colour of the text on the console.ForegroundColor:

Console.ForegroundColor = ConsoleColor.Red;


Console.WriteLine("This message is in red.");


Console.ResetColor(); // Reset to default color

Error Reporting

You may use various colours for different sorts of messages to distinguish between ordinary logs and error messages:

Console.ForegroundColor = ConsoleColor.Red;


Console.WriteLine("Error message");


Console.ResetColor();

Input from the Console

Using Console, you may read user input.ReadLine():

Console.Write("Enter your name: ");


string name = Console.ReadLine();


Console.WriteLine($"Hello, {name}!");

Output Formatting

You may alter how data is displayed by using formatting settings. In the Console, for example, you may utilise placeholders. Method WriteLine:

int number = 42;


Console.WriteLine("The answer to life, the universe, and everything is {0}", number);

Programme :

using System;


class Program


{


    static void Main()


    {


        // Simple console log


        Console.WriteLine("This is a console log message.");


        // String interpolation for dynamic messages


        string name = "Alice";


        int age = 25;


        Console.WriteLine($"Hello, my name is {name} and I am {age} years old.");


        // Changing text color for emphasis


        Console.ForegroundColor = ConsoleColor.Red;


        Console.WriteLine("This is an error message in red.");


        Console.ResetColor(); // Reset color to default


        // Reading input from user


        Console.Write("Enter your favorite color: ");


        string favoriteColor = Console.ReadLine();


        Console.WriteLine($"Your favorite color is {favoriteColor}.");


        // Formatting output


        int num1 = 10;


        double num2 = 3.14;


        Console.WriteLine("Formatted Output: {0} and {1:F2}", num1, num2);


        // Error logging


        Console.ForegroundColor = ConsoleColor.Red;


        Console.WriteLine("An error occurred!");


        Console.ResetColor();


        // Redirecting console output to a file


        using (System.IO.StreamWriter writer = new System.IO.StreamWriter("log.txt"))


        {


            Console.SetOut(writer);


            Console.WriteLine("This message is written to log.txt");


        }


        // Console log for debugging


        int x = 5;


        int y = 7;


        Console.WriteLine($"The value of x is {x} and the value of y is {y}");


    }


}

Output:

CONSOLE LOG IN C#