using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.Logging { /// /// Represents an file logger. /// /// public class FileLogger : ILogger { private DateTime _logFileDate; /// /// Gets the logs folder. /// public static String DefaultLogsFolder { get; private set; } /// /// Gets or sets the log file. /// public String LogFile { get; private set; } /// /// Gets the tag name which will be appended to the file. /// public String Tag { get; private set; } /// /// Gets or sets the log file folder. /// public String Folder { get; private set; } /// /// Initializes the class. /// static FileLogger() { DefaultLogsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Logs", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName)); } /// /// Initializes a new instance of the class. /// public FileLogger() { _isEnabled = true; Tag = Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName); Folder = DefaultLogsFolder; Directory.CreateDirectory(Folder); LogFile = CreateLogFileName(); } /// /// Initializes a new instance of the class. /// /// Logs folder path. /// The tag name which will be appended to the file name. public FileLogger(String folder, String tag) : this() { Folder = folder; Tag = tag; Directory.CreateDirectory(Folder); LogFile = CreateLogFileName(); } /// /// Called when a new library trace is available. /// /// The output. public void OnLog(LogItemBase output) { try { if (DateTime.Now.Date > _logFileDate.Date) { File.AppendAllText(LogFile, Environment.NewLine + Environment.NewLine + "### This log file continues on the next log file ###" + Environment.NewLine); LogFile = CreateLogFileName(); File.AppendAllText(LogFile, "### This log file is a continuation of a previous log file ###" + Environment.NewLine + Environment.NewLine); } File.AppendAllText(LogFile, output.ToString() + Environment.NewLine); } catch { Debug.WriteLine("Error Writing To Log File!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } } private bool _isEnabled; /// /// Gets or sets a value indicating whether this is enabled. /// public bool Enabled { get { return _isEnabled; } set { _isEnabled = value; } } /// /// Creates the name of the log file. /// /// private String CreateLogFileName() { _logFileDate = DateTime.Now.Date; return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}.log", DateTime.Now, Tag)); } } }