Write Cleaned Audit Lines — StreamWriter + Append
Scenario: You receive audit lines (login/create) that include extra spaces and noisy symbols. You must clean them and append to a log file. What to implement: - AuditFile.AppendCleanLines(string filePath, List<string> lines) Rules: - Validate filePath not empty - For each line: trim; skip empty - Replace multiple spaces with single space - Append each cleaned line + newline to the file ✅ Implement ONLY the TODO method.
using System; // Console
using System.Collections.Generic; // List
using System.IO; // File IO
using System.Text.RegularExpressions; // Regex
namespace ItTechGenie.M1.FileIO.Q1
{
public static class AuditFile
{
// ✅ TODO: Student must implement only this method
public static void AppendCleanLines(string filePath, List<string> lines)
{
// TODO:
// - validate inputs
// - ensure directory exists
// - open StreamWriter in append mode
// - clean each line and write
throw new NotImplementedException();
}
}
internal class Program
{
static void Main()
{
var path = @".\data\audit log ✅.txt";
var lines = new List<string>
{
"[2026-02-18 10:05] USER='Sana @ Chennai' ACTION='LOGIN' IP='10.0.0.5' ",
" ",
"[2026-02-18 10:06] USER='Ravi' ACTION='CREATE' SKU='SKU-β77' "
};
AuditFile.AppendCleanLines(path, lines);
Console.WriteLine("Done ✅");
}
}
}