Logger - C# Programming Exercise

In this exercise of C#, you need to create a Logger class with a static Write method, which will append a certain text to a log file. The method should take the file name and the text to be logged as parameters and include the current date and time before the text, all on the same line, to make the log file easier to analyze. The Write method should use the AppendText method to append the text to the file and use DateTime.Now to get the current date and time. This exercise is an excellent way to learn how to work with text files in C#, as well as with system date and time. Additionally, it helps develop skills to create useful log files in software development.

Through this exercise, programmers will learn how to append entries with date and time to a file, a common skill in creating applications that require tracking events or errors.

 Category

File Management

 Exercise

Logger

 Objective

Create a class Logger, with a static method Write, which will append a certain text to a file: Logger.Write("myLog.txt", "This text is being logged");

It must also include the current date and time before the text (in the same line), so that the log file is easier to analyze.

Hint: find information about "AppendText" and about "DateTime.now"

 Write Your C# Exercise

// Importing necessary namespaces for file handling and date/time manipulation
using System;
using System.IO;

public class Logger
{
    // Static method to append a log entry to a specified file
    public static void Write(string fileName, string message)
    {
        // Formatting the message with the current date and time
        string logEntry = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message;

        // Using StreamWriter in append mode to write the log entry to the file
        using (StreamWriter writer = File.AppendText(fileName))
        {
            writer.WriteLine(logEntry);  // Writing the formatted log entry to the file
        }

        // Confirming the log entry has been written to the file
        Console.WriteLine("Log entry successfully written to file: " + fileName);
    }
}

// Auxiliary class for testing the Logger functionality
public class Program
{
    public static void Main()
    {
        // Example usage of the Logger's Write method
        Logger.Write("myLog.txt", "This text is being logged");

        // Logging another message to demonstrate multiple entries
        Logger.Write("myLog.txt", "Another log entry added");
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  More

    In this exercise of C#, you need to create a program that behaves like the Unix "more" command. The program should display the contents of a text file and prom...

  •  Text replacer

    In this exercise of C#, you need to create a program to replace words in a text file, saving the result into a new file. The program should take as parameters the fil...

  •  Count letters in a file

    In this exercise of C#, you need to create a program to count how many times a specific character appears inside a file of any kind. The file and the letter to search...

  •  Reading a binary file (1: BMP)

    In this exercise of C#, you need to create a program that checks if a BMP image file seems to be correct. The program must verify if the first two bytes of the file ...

  •  Writing to a binary file

    In this exercise of C#, you need to create a program that asks the user for their name, age (as a byte), and the year they were born (as an int), and st...

  •  C# to Java

    In this exercise of C#, you need to create a basic C# to Java translator. The program should accept C# source files and generate an equivalent ...