FtpTrace.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace System.Net.FtpClient {
  5. /// <summary>
  6. /// Used for transaction logging and debug information.
  7. /// </summary>
  8. /// <example>The following example illustrates how to assist in debugging
  9. /// System.Net.FtpClient by getting a transaction log from the server.
  10. /// <code source="..\Examples\Debug.cs" lang="cs" />
  11. /// </example>
  12. public static class FtpTrace {
  13. static List<TraceListener> m_listeners = new List<TraceListener>();
  14. static bool m_flushOnWrite = false;
  15. /// <summary>
  16. /// Gets or sets whether the trace listeners should be flushed or not
  17. /// after writing to them. Default value is false.
  18. /// </summary>
  19. public static bool FlushOnWrite {
  20. get {
  21. return m_flushOnWrite;
  22. }
  23. set {
  24. m_flushOnWrite = value;
  25. }
  26. }
  27. /// <summary>
  28. /// Add a TraceListner to the collection. You can use one of the predefined
  29. /// TraceListeners in the System.Diagnostics namespace, such as ConsoleTraceListener
  30. /// for logging to the console, or you can write your own deriving from
  31. /// System.Diagnostics.TraceListener.
  32. /// </summary>
  33. /// <param name="listener">The TraceListener to add to the collection</param>
  34. public static void AddListener(TraceListener listener) {
  35. lock (m_listeners) {
  36. m_listeners.Add(listener);
  37. }
  38. }
  39. /// <summary>
  40. /// Remove the specified TraceListener from the collection
  41. /// </summary>
  42. /// <param name="listener">The TraceListener to remove from the collection.</param>
  43. public static void RemoveListener(TraceListener listener) {
  44. lock (m_listeners) {
  45. m_listeners.Remove(listener);
  46. }
  47. }
  48. /// <summary>
  49. /// Write to the TraceListeners.
  50. /// </summary>
  51. /// <param name="message">The message to write</param>
  52. /// <param name="args">Optional variables if using a format string similar to string.Format()</param>
  53. public static void Write(string message, params object[] args) {
  54. Write(string.Format(message, args));
  55. }
  56. /// <summary>
  57. /// Write to the TraceListeners
  58. /// </summary>
  59. /// <param name="message">The message to write</param>
  60. public static void Write(string message) {
  61. TraceListener[] listeners;
  62. lock (m_listeners) {
  63. listeners = m_listeners.ToArray();
  64. }
  65. #if DEBUG
  66. Debug.Write(message);
  67. #endif
  68. foreach (TraceListener t in listeners) {
  69. t.Write(message);
  70. if (m_flushOnWrite) {
  71. t.Flush();
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Write to the TraceListeners.
  77. /// </summary>
  78. /// <param name="message">The message to write</param>
  79. /// <param name="args">Optional variables if using a format string similar to string.Format()</param>
  80. public static void WriteLine(string message, params object[] args) {
  81. Write(string.Format("{0}{1}", string.Format(message, args), Environment.NewLine));
  82. }
  83. /// <summary>
  84. /// Write to the TraceListeners
  85. /// </summary>
  86. /// <param name="message">The message to write</param>
  87. public static void WriteLine(string message) {
  88. Write(string.Format("{0}{1}", message, Environment.NewLine));
  89. }
  90. }
  91. }