Tuesday, November 7, 2017

Create Log in c#

We can easliy create log by using File.AppendAllText() method because by using this method, it is guaranteed that the file handle will be closed, even if exceptions are raised.
1
2
3
4
5
6
7
public static class Logger
{
    public static void Log(string message)
    {
        File.AppendAllText("C:\\LogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine);
    }
}