using System; using System.Collections.Generic; using System.Diagnostics; namespace FluentFTP { /// /// Used for transaction logging and debug information. /// /// The following example illustrates how to assist in debugging /// FluentFTP by getting a transaction log from the server. /// /// public static class FtpTrace { #if !CORE static List m_listeners = new List(); static bool m_flushOnWrite = false; /// /// Gets or sets whether the trace listeners should be flushed or not /// after writing to them. Default value is false. /// public static bool FlushOnWrite { get { return m_flushOnWrite; } set { m_flushOnWrite = value; } } /// /// Add a TraceListner to the collection. You can use one of the predefined /// TraceListeners in the System.Diagnostics namespace, such as ConsoleTraceListener /// for logging to the console, or you can write your own deriving from /// System.Diagnostics.TraceListener. /// /// The TraceListener to add to the collection public static void AddListener(TraceListener listener) { lock (m_listeners) { m_listeners.Add(listener); } } /// /// Remove the specified TraceListener from the collection /// /// The TraceListener to remove from the collection. public static void RemoveListener(TraceListener listener) { lock (m_listeners) { m_listeners.Remove(listener); } } #endif /// /// Write to the TraceListeners. /// /// The message to write /// Optional variables if using a format string similar to string.Format() public static void Write(string message, params object[] args) { Write(string.Format(message, args)); } /// /// Write to the TraceListeners /// /// The message to write public static void Write(string message) { #if !CORE TraceListener[] listeners; lock (m_listeners) { listeners = m_listeners.ToArray(); } #endif #if DEBUG Debug.Write(message); #endif #if !CORE foreach (TraceListener t in listeners) { t.Write(message); if (m_flushOnWrite) { t.Flush(); } } #endif } /// /// Write to the TraceListeners. /// /// The message to write /// Optional variables if using a format string similar to string.Format() public static void WriteLine(string message, params object[] args) { Write(string.Format("{0}{1}", string.Format(message, args), Environment.NewLine)); } /// /// Write to the TraceListeners /// /// The message to write public static void WriteLine(string message) { Write(string.Format("{0}{1}", message, Environment.NewLine)); } } }