Proposed exercise
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"
Output
Solution
using System;
using System.IO;
class Logger
{
public static void Write(string name, string text)
{
try
{
StreamWriter file = File.AppendText(name);
file.WriteLine(DateTime.Now + " - " + text);
file.Close();
}
catch (Exception)
{
Console.WriteLine("Error!!!");
}
}
}
class Test
{
static void Main()
{
Logger.Write("text.txt", "Hola");
}
}