/// <summary> /// Create and open a new file logger for the given hub. /// </summary> /// <param name="hub">The hub to log messages from.</param> /// <param name="path">The path to the folder to place log files in.</param> /// <param name="saveOld">If old log files should be kept.</param> public FileLogger(EventHub hub, string path = "./logs", bool saveOld = true) : base(hub) { string filename = Path.Combine(path, "latest.log"); var fileInfo = new FileInfo(filename); // Create directory, if needed if (fileInfo == null || !fileInfo.Exists) { Directory.CreateDirectory(Path.GetDirectoryName(filename) ?? path); fileInfo = new FileInfo(filename); } // Save old file, if needed if (saveOld && fileInfo.Exists) { fileInfo.MoveTo(fileInfo.FullName.Replace("latest", fileInfo.CreationTime.ToString("yyMMdd_HHmmss"))); } // Open the file _writer = new StreamWriter(File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read), encoding: Encoding.UTF8, leaveOpen: false); FilePath = (_writer.BaseStream as FileStream) !.Name; // Write initial file contents _writer.Write($"Log opened - {DateTime.Now.ToString("G")}\n\n"); _writer.Flush(); }
protected LogListener(EventHub hub) { Subscription = hub.Subscribe <LogEvent>(HandleLogEvent); }