Skip to content

Instantly share code, notes, and snippets.

@FusRoDah061
Created February 22, 2019 18:09
Show Gist options
  • Save FusRoDah061/e8115483d52e1768e90644b5ed404c02 to your computer and use it in GitHub Desktop.
Save FusRoDah061/e8115483d52e1768e90644b5ed404c02 to your computer and use it in GitHub Desktop.
public class Logger
{
public static void Log(object msg, bool newLine = true)
{
string message = "<" + DateTime.Now.ToString() + "> " + msg.ToString() + ((newLine) ? "\n" : "");
Console.Write(message);
LogFile(message, _getFileName());
}
public static void Log(Exception ex)
{
string message = "<" + DateTime.Now.ToString() + "> " + ex.ToString();
Console.Write(message);
LogFile(message, _getFileName());
}
public static void LogFile(object msg, string filename, bool append = true)
{
try
{
if (append)
{
File.AppendAllText(filename, "\r\n<" + DateTime.Now.ToString() + "> " + msg.ToString());
}
else
{
File.WriteAllText(filename, "\r\n<" + DateTime.Now.ToString() + "> " + msg.ToString());
}
} catch { }
}
private static string _getFileName()
{
return "log_" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + ".log";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment