WebTrace.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // System.Web.Util.WebTrace
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.Collections;
  10. using System.Diagnostics;
  11. namespace System.Web.Util
  12. {
  13. internal class WebTrace
  14. {
  15. static Stack ctxStack;
  16. static bool trace;
  17. static WebTrace ()
  18. {
  19. ctxStack = new Stack ();
  20. }
  21. [Conditional("WEBTRACE")]
  22. static public void PushContext (string context)
  23. {
  24. ctxStack.Push (context);
  25. Trace.Indent ();
  26. }
  27. [Conditional("WEBTRACE")]
  28. static public void PopContext ()
  29. {
  30. if (ctxStack.Count == 0)
  31. return;
  32. Trace.Unindent ();
  33. ctxStack.Pop ();
  34. }
  35. static public string Context
  36. {
  37. get {
  38. if (ctxStack.Count == 0)
  39. return "No context";
  40. return (string) ctxStack.Peek ();
  41. }
  42. }
  43. static public bool StackTrace
  44. {
  45. get { return trace; }
  46. set { trace = value; }
  47. }
  48. [Conditional("WEBTRACE")]
  49. static public void WriteLine (string msg)
  50. {
  51. Trace.WriteLine (Format (msg));
  52. }
  53. [Conditional("WEBTRACE")]
  54. static public void WriteLine (string msg, object arg)
  55. {
  56. Trace.WriteLine (Format (String.Format (msg, arg)));
  57. }
  58. [Conditional("WEBTRACE")]
  59. static public void WriteLine (string msg, object arg1, object arg2)
  60. {
  61. Trace.WriteLine (Format (String.Format (msg, arg1, arg2)));
  62. }
  63. [Conditional("WEBTRACE")]
  64. static public void WriteLine (string msg, object arg1, object arg2, object arg3)
  65. {
  66. Trace.WriteLine (Format (String.Format (msg, arg1, arg2, arg3)));
  67. }
  68. [Conditional("WEBTRACE")]
  69. static public void WriteLine (string msg, params object [] args)
  70. {
  71. Trace.WriteLine (Format (String.Format (msg, args)));
  72. }
  73. static string Format (string msg)
  74. {
  75. if (trace)
  76. return String.Format ("{0}: {1}\n{2}", Context, msg, Environment.StackTrace);
  77. else
  78. return String.Format ("{0}: {1}", Context, msg);
  79. }
  80. }
  81. }