Last active
December 7, 2015 14:01
-
-
Save yalayabeeb/b861b4184575984c59fd to your computer and use it in GitHub Desktop.
A class used to create and write to a log file in order to report errors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
public class Log | |
{ | |
public string FilePath { get; set; } | |
public bool UseCustomFormat { get; set; } | |
public string Format { get; set; } | |
public Log(string filePath) | |
{ | |
FilePath = filePath; | |
if (!File.Exists(FilePath)) | |
File.Create(filePath).Dispose(); | |
} | |
public bool Write(string text) | |
{ | |
try | |
{ | |
if (!UseCustomFormat) | |
Format = DateTime.Now.ToString("dd/MM/yy HH:mm:ss") + " - "; | |
string contents = Format + text; | |
File.AppendAllLines(FilePath, new string[] { contents }); | |
return true; | |
} | |
catch (Exception) { return false; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment