using System; using System.IO; namespace StardewModdingAPI.Framework.Logging { /// Manages reading and writing to log file. internal class LogFileManager : IDisposable { /********* ** Properties *********/ /// The underlying stream writer. private readonly StreamWriter Stream; /********* ** Public methods *********/ /// Construct an instance. /// The log file to write. public LogFileManager(string path) { // create log directory if needed string logDir = Path.GetDirectoryName(path); if (logDir == null) throw new ArgumentException($"The log path '{path}' is not valid."); Directory.CreateDirectory(logDir); // open log file stream this.Stream = new StreamWriter(path, append: false) { AutoFlush = true }; } /// Write a message to the log. /// The message to log. public void WriteLine(string message) { // always use Windows-style line endings for convenience // (Linux/Mac editors are fine with them, Windows editors often require them) this.Stream.Write(message + "\r\n"); } /// Release all resources. public void Dispose() { this.Stream.Dispose(); } } }