Skip to content

Instantly share code, notes, and snippets.

@yalayabeeb
Last active December 7, 2015 14:01
Show Gist options
  • Save yalayabeeb/b861b4184575984c59fd to your computer and use it in GitHub Desktop.
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.
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