Practice file handling and exception handling together. You'll write a logger that saves timestamped messages to a file and reads them back.
YOUR TASK
Create a program that:
Writes 3 log messages to a file called log.txt using File.AppendAllText
Each message should include a prefix like [INFO] or [ERROR]
Reads the file back and prints all lines to the console
Wraps file operations in a try/catch block
Need a hint?
Key APIs:
1. Write: File.AppendAllText("log.txt", "[INFO] msg\n");
2. Read all lines: string[] lines = File.ReadAllLines("log.txt");
3. Wrap in: try { ... } catch (Exception e) { Console.WriteLine(e.Message); }
4. Add using System.IO; at the top
Expected output:
Log written successfully.
Reading log file...
[INFO] App started
[INFO] User logged in
[ERROR] Something went wrong
Code Editor
Ready
Output
Console
Console initialized...
Ready for Project 3: File Logger
> Test your code with the "Check My Code" button
> When it passes, the Complete button will activate!