8.05 - Logger

Objetive

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"


Solution

using System;
using System.IO;
namespace LoggerAplication
{
    class Logger
    {
        public static void Write(string nameFile,string text) 
        {
            StreamWriter myFile;
            myFile = File.AppendText(nameFile);
            myFile.WriteLine(DateTime.Now + " - " + text);
            myFile.Close();
        }
    }
}


using System;
namespace LoggerAplication
{
    class Program
    {
        static void Main()
        {
            Logger.Write("text.txt", "Hola");
        }
    }
}


Popular Posts