TraceHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // System.Web.Handlers.TraceHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Jackson Harper ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  9. // (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.Data;
  33. using System.Web;
  34. using System.Web.Util;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. namespace System.Web.Handlers
  38. {
  39. public class TraceHandler : IHttpHandler
  40. {
  41. void IHttpHandler.ProcessRequest (HttpContext context)
  42. {
  43. TraceManager manager = HttpRuntime.TraceManager;
  44. if (manager.LocalOnly && !context.Request.IsLocal) {
  45. // Need to figure out the error message that goes here
  46. // but I only have cassini for testing
  47. return;
  48. }
  49. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  50. if (context.Request.QueryString ["clear"] != null)
  51. manager.Clear ();
  52. string id_str = context.Request.QueryString ["id"];
  53. int id = -1;
  54. if (id_str != null)
  55. id = Int32.Parse (id_str);
  56. if (id > 0 && id <= manager.ItemCount) {
  57. RenderItem (manager, output, id);
  58. } else {
  59. string dir = context.Server.MapPath (UrlUtils.GetDirectory (context.Request.FilePath));
  60. RenderMenu (manager, output, dir);
  61. }
  62. }
  63. bool IHttpHandler.IsReusable
  64. {
  65. get {
  66. return false;
  67. }
  68. }
  69. private void RenderMenu (TraceManager manager, HtmlTextWriter output, string dir)
  70. {
  71. output.RenderBeginTag (HtmlTextWriterTag.Html);
  72. output.RenderBeginTag (HtmlTextWriterTag.Head);
  73. TraceData.RenderStyleSheet (output);
  74. output.RenderEndTag ();
  75. RenderHeader (output, dir);
  76. output.RenderBeginTag (HtmlTextWriterTag.Body);
  77. output.AddAttribute ("class", "tracecontent");
  78. output.RenderBeginTag (HtmlTextWriterTag.Span);
  79. Table table = TraceData.CreateTable ();
  80. table.Rows.Add (TraceData.AltRow ("Requests to the Application"));
  81. table.Rows.Add (TraceData.SubHeadRow ("No", "Time of Request",
  82. "File", "Status Code", "Verb", "&nbsp;"));
  83. if (manager.TraceData != null) {
  84. for (int i=0; i<manager.ItemCount; i++) {
  85. int item = i + 1;
  86. TraceData d = manager.TraceData [i];
  87. TraceData.RenderAltRow (table, i, item.ToString (), d.RequestTime.ToString (),
  88. d.RequestPath, d.StatusCode.ToString (), d.RequestType,
  89. "<a href=\"Trace.axd?id=" + item + "\" class=\"tinylink\">" +
  90. "<b><nobr>View Details</a>");
  91. }
  92. table.RenderControl (output);
  93. }
  94. output.RenderEndTag ();
  95. output.RenderEndTag ();
  96. output.RenderEndTag ();
  97. }
  98. private void RenderHeader (HtmlTextWriter output, string dir)
  99. {
  100. Table table = TraceData.CreateTable ();
  101. TableRow row1 = new TableRow ();
  102. TableRow row2 = new TableRow ();
  103. TableCell cell1 = new TableCell ();
  104. TableCell cell2 = new TableCell ();
  105. TableCell cell3 = new TableCell ();
  106. TableCell cell4 = new TableCell ();
  107. cell1.Text = "<h1>Application Trace</h1>";
  108. cell2.Text = "[ <a href=\"Trace.axd?clear=1\" class=\"link\">clear current trace</a> ]";
  109. cell2.HorizontalAlign = HorizontalAlign.Right;
  110. cell2.VerticalAlign = VerticalAlign.Bottom;
  111. row1.Cells.Add (cell1);
  112. row1.Cells.Add (cell2);
  113. cell3.Text = "<h2><h2><p>"; // ummm, WTF?
  114. cell4.Text = "<b>Physical Directory:</b>" + dir;
  115. row2.Cells.Add (cell3);
  116. row2.Cells.Add (cell4);
  117. table.Rows.Add (row1);
  118. table.Rows.Add (row2);
  119. table.RenderControl (output);
  120. }
  121. private void RenderItem (TraceManager manager, HtmlTextWriter output, int item)
  122. {
  123. manager.TraceData [item - 1].Render (output);
  124. }
  125. [MonoTODO ("Appears in class status, but...")]
  126. protected void ShowDetails (DataSet data)
  127. {
  128. }
  129. [MonoTODO ("Appears in class status, but...")]
  130. protected void ShowRequests (ArrayList list)
  131. {
  132. }
  133. }
  134. }