2 // System.Web.TraceData
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
38 using System.Web.UI.WebControls;
40 namespace System.Web {
42 internal class TraceData {
44 private bool is_first_time;
45 private DateTime first_time;
46 private double prev_time;
48 private DataTable info;
49 private DataTable control_data;
50 private DataTable cookie_data;
51 private DataTable header_data;
52 private DataTable servervar_data;
53 //private DataTable viewstate_data;
55 private string request_path;
56 private string session_id;
57 private DateTime request_time;
58 private Encoding request_encoding;
59 private Encoding response_encoding;
60 private string request_type;
61 private int status_code;
66 info = new DataTable ();
67 info.Columns.Add (new DataColumn ("Category", typeof (string)));
68 info.Columns.Add (new DataColumn ("Message", typeof (string)));
69 info.Columns.Add (new DataColumn ("Exception", typeof (string)));
70 info.Columns.Add (new DataColumn ("TimeSinceFirst", typeof (double)));
71 info.Columns.Add (new DataColumn ("IsWarning", typeof (bool)));
73 control_data = new DataTable ();
74 control_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
75 control_data.Columns.Add (new DataColumn ("Type", typeof (System.Type)));
76 control_data.Columns.Add (new DataColumn ("RenderSize", typeof (int)));
77 control_data.Columns.Add (new DataColumn ("ViewstateSize", typeof (int)));
78 control_data.Columns.Add (new DataColumn ("Depth", typeof (int)));
80 cookie_data = new DataTable ();
81 cookie_data.Columns.Add (new DataColumn ("Name", typeof (string)));
82 cookie_data.Columns.Add (new DataColumn ("Value", typeof (string)));
84 header_data = new DataTable ();
85 header_data.Columns.Add (new DataColumn ("Name", typeof (string)));
86 header_data.Columns.Add (new DataColumn ("Value", typeof (string)));
88 servervar_data = new DataTable ();
89 servervar_data.Columns.Add (new DataColumn ("Name", typeof (string)));
90 servervar_data.Columns.Add (new DataColumn ("Value", typeof (string)));
93 viewstate_data = new DataTable ();
94 viewstate_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
95 viewstate_data.Columns.Add (new DataColumn ("Data", typeof (string)));
101 public string RequestPath {
102 get { return request_path; }
103 set { request_path = value; }
106 public string SessionID {
107 get { return session_id; }
108 set { session_id = value; }
111 public DateTime RequestTime {
112 get { return request_time; }
113 set { request_time = value; }
116 public Encoding RequestEncoding {
117 get { return request_encoding; }
118 set { request_encoding = value; }
121 public Encoding ResponseEncoding {
122 get { return response_encoding; }
123 set { response_encoding = value; }
126 public string RequestType {
127 get { return request_type; }
128 set { request_type = value; }
131 public int StatusCode {
132 get { return status_code; }
133 set { status_code = value; }
136 public void Write (string category, string msg, Exception error, bool Warning)
141 is_first_time = false;
142 first_time = DateTime.Now;
144 time = (DateTime.Now - first_time).TotalSeconds;
146 DataRow r = info.NewRow ();
147 r ["Category"] = category;
148 r ["Message"] = HtmlEncode (msg);
149 r ["Exception"] = (error != null ? error.ToString () : null);
150 r ["TimeSinceFirst"] = time;
151 r ["IsWarning"] = Warning;
156 static string HtmlEncode (string s)
161 string res = HttpUtility.HtmlEncode (s);
162 res = res.Replace ("\n", "<br>");
163 return res.Replace (" ", " ");
166 public void AddControlTree (Page page, Hashtable ctrl_vs, Hashtable sizes)
169 this.ctrl_vs = ctrl_vs;
171 AddControl (page, 0);
176 void AddControl (Control c, int control_pos)
178 DataRow r = control_data.NewRow ();
179 r ["ControlId"] = c.UniqueID;
180 r ["Type"] = c.GetType ();
181 r ["Depth"] = control_pos;
182 r ["RenderSize"] = GetRenderSize (c);
183 r ["ViewstateSize"] = GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null);
185 control_data.Rows.Add (r);
187 if (c.HasControls ()) {
188 foreach (Control child in c.Controls)
189 AddControl (child, control_pos + 1);
193 int GetRenderSize (Control ctrl)
198 object s = sizes [ctrl];
199 return s == null ? 0 : (int) s;
202 static int GetViewStateSize (Control ctrl, object vs)
207 StringWriter sr = new StringWriter ();
208 LosFormatter fmt = new LosFormatter ();
209 fmt.Serialize (sr, vs);
210 return sr.GetStringBuilder ().Length;
213 public void AddCookie (string name, string value)
215 DataRow r = cookie_data.NewRow ();
220 cookie_data.Rows.Add (r);
223 public void AddHeader (string name, string value)
225 DataRow r = header_data.NewRow ();
230 header_data.Rows.Add (r);
233 public void AddServerVar (string name, string value)
235 DataRow r = servervar_data.NewRow ();
240 servervar_data.Rows.Add (r);
243 public void Render (HtmlTextWriter output)
245 output.AddAttribute ("id", "__asptrace");
246 output.RenderBeginTag (HtmlTextWriterTag.Div);
248 RenderStyleSheet (output);
250 output.AddAttribute ("class", "tracecontent");
251 output.RenderBeginTag (HtmlTextWriterTag.Span);
253 RenderRequestDetails (output);
254 RenderTraceInfo (output);
255 RenderControlTree (output);
256 RenderCookies (output);
257 RenderHeaders (output);
258 RenderServerVars (output);
260 output.RenderEndTag ();
261 output.RenderEndTag ();
264 private void RenderRequestDetails (HtmlTextWriter output)
266 Table table = CreateTable ();
268 table.Rows.Add (AltRow ("Request Details:"));
269 table.Rows.Add (InfoRow2 ("Session Id:", session_id,
270 "Request Type", request_type));
271 table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
272 "State Code:", status_code.ToString ()));
273 table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
274 "Response Encoding:", response_encoding.EncodingName));
275 table.RenderControl (output);
278 private void RenderTraceInfo (HtmlTextWriter output)
280 Table table = CreateTable ();
282 table.Rows.Add (AltRow ("Trace Information"));
283 table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
286 foreach (DataRow r in info.Rows)
287 RenderTraceInfoRow (table, r, pos++);
289 table.RenderControl (output);
292 private void RenderControlTree (HtmlTextWriter output)
294 Table table = CreateTable ();
296 int page_vs_size = page == null ? 0 : GetViewStateSize (page, page.GetSavedViewState ());
297 table.Rows.Add (AltRow ("Control Tree"));
298 table.Rows.Add (SubHeadRow ("Control Id", "Type",
299 "Render Size Bytes (including children)",
300 String.Format ("View state Size (total: {0} bytes)(excluding children)",
304 foreach (DataRow r in control_data.Rows) {
305 int depth = (int) r ["Depth"];
306 string prefix = String.Empty;
307 for (int i=0; i<depth; i++)
308 prefix += " ";
309 RenderAltRow (table, pos++, prefix + r ["ControlId"],
310 r ["Type"].ToString (), r ["RenderSize"].ToString (),
311 r ["ViewstateSize"].ToString ());
314 table.RenderControl (output);
317 private void RenderCookies (HtmlTextWriter output)
319 Table table = CreateTable ();
321 table.Rows.Add (AltRow ("Cookies Collection"));
322 table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
325 foreach (DataRow r in cookie_data.Rows) {
326 string name = r ["Name"].ToString ();
327 string value = r ["Value"].ToString ();
328 int length = name.Length + (value == null ? 0 : value.Length);
329 RenderAltRow (table, pos++, name, value, length.ToString ());
332 table.RenderControl (output);
335 private void RenderHeaders (HtmlTextWriter output)
337 Table table = CreateTable ();
339 table.Rows.Add (AltRow ("Headers Collection"));
340 table.Rows.Add (SubHeadRow ("Name", "Value"));
343 foreach (DataRow r in header_data.Rows)
344 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
346 table.RenderControl (output);
349 private void RenderServerVars (HtmlTextWriter output)
351 Table table = CreateTable ();
353 table.Rows.Add (AltRow ("Server Variables"));
354 table.Rows.Add (SubHeadRow ("Name", "Value"));
357 foreach (DataRow r in servervar_data.Rows)
358 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
360 table.RenderControl (output);
363 internal static TableRow AltRow (string title)
365 TableRow row = new TableRow ();
366 TableHeaderCell header = new TableHeaderCell ();
367 header.CssClass = "alt";
368 header.HorizontalAlign = HorizontalAlign.Left;
369 header.Attributes [" colspan"] = "10";
370 header.Text = "<h3><b>" + title + "</b></h3>";
372 row.Cells.Add (header);
376 private TableRow RenderTraceInfoRow (Table table, DataRow r, int pos)
379 open = close = String.Empty;
380 if ((bool) r ["IsWarning"]) {
381 open = "<font color=\"Red\">";
385 double t = (double) r ["TimeSinceFirst"];
388 t1 = t2 = String.Empty;
391 t1 = t.ToString ("0.000000");
392 t2 = (t - prev_time).ToString ("0.000000");
396 return RenderAltRow (table, pos, open + (string) r ["Category"] + close,
397 open + (string) r ["Message"] + close, t1, t2);
400 internal static TableRow SubHeadRow (params string[] cells)
402 TableRow row = new TableRow ();
403 foreach (string s in cells) {
404 TableHeaderCell cell = new TableHeaderCell ();
406 row.Cells.Add (cell);
409 row.CssClass = "subhead";
410 row.HorizontalAlign = HorizontalAlign.Left;
415 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
417 TableRow row = new TableRow ();
418 foreach (string s in cells) {
419 TableCell cell = new TableCell ();
421 row.Cells.Add (cell);
425 row.CssClass = "alt";
427 table.Rows.Add (row);
431 private TableRow InfoRow2 (string title1, string info1, string title2, string info2)
433 TableRow row = new TableRow ();
434 TableHeaderCell header1 = new TableHeaderCell ();
435 TableHeaderCell header2 = new TableHeaderCell ();
436 TableCell cell1 = new TableCell ();
437 TableCell cell2 = new TableCell ();
439 header1.Text = title1;
440 header2.Text = title2;
444 row.Cells.Add (header1);
445 row.Cells.Add (cell1);
446 row.Cells.Add (header2);
447 row.Cells.Add (cell2);
449 row.HorizontalAlign = HorizontalAlign.Left;
454 internal static Table CreateTable ()
456 Table table = new Table ();
458 table.Width = Unit.Percentage (100);
459 table.CellSpacing = 0;
460 table.CellPadding = 0;
465 internal static void RenderStyleSheet (HtmlTextWriter o)
467 o.WriteLine ("<style type=\"text/css\">");
468 o.Write ("span.tracecontent { background-color:white; ");
469 o.WriteLine ("color:black;font: 10pt verdana, arial; }");
470 o.Write ("span.tracecontent table { font: 10pt verdana, ");
471 o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
472 o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
473 o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
474 o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
475 o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
476 o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
477 o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
478 o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
479 o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
480 o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
481 o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
482 o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
483 o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
484 o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
485 o.WriteLine ("color:white; padding:0,5,0,5; }");
486 o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
487 o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
488 o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
489 o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
490 o.WriteLine ("arial;text-decoration:underline;}");
491 o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
492 o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
493 o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
494 o.WriteLine ("span.tracecontent table td { padding-right:20 }");
495 o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
496 o.WriteLine ("</style>");