Skip to content

Instantly share code, notes, and snippets.

@SamKr
Last active October 24, 2018 16:01
Show Gist options
  • Save SamKr/b5f445d81ccb3a1d879fcdffc479dabd to your computer and use it in GitHub Desktop.
Save SamKr/b5f445d81ccb3a1d879fcdffc479dabd to your computer and use it in GitHub Desktop.
Log text to file
if (!Directory.Exists(Variables.LogDirectory)) Directory.CreateDirectory(Variables.LogDirectory);
var logFile = $"[{DateTime.Now:yyyy-MM-dd}] Log.txt";
if (error) logFile = $"[{DateTime.Now:yyyy-MM-dd}] ErrorLog.txt";
logFile = Path.Combine(Variables.LogDirectory, logFile);
using (var stream = new FileStream(logFile, FileMode.Append, FileAccess.Write))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine($"[{DateTime.Now:O}] {msg}");
writer.Close();
stream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment