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;
37 using System.Web.UI.WebControls;
40 using System.Collections.Generic;
43 namespace System.Web {
47 public string Category;
48 public string Message;
49 public string Exception;
50 public double TimeSinceFirst;
51 public double TimeSinceLast;
52 public bool IsWarning;
54 public InfoTraceData (string category, string message, string exception, double timeSinceFirst, double timeSinceLast, bool isWarning)
56 this.Category = category;
57 this.Message = message;
58 this.Exception = exception;
59 this.TimeSinceFirst = timeSinceFirst;
60 this.TimeSinceLast = timeSinceLast;
61 this.IsWarning = isWarning;
65 class ControlTraceData
67 public string ControlId;
69 public int RenderSize;
70 public int ViewstateSize;
73 public int ControlstateSize;
77 public ControlTraceData (string controlId, Type type, int renderSize, int viewstateSize, int controlstateSize, int depth)
79 public ControlTraceData (string controlId, Type type, int renderSize, int viewstateSize, int depth)
82 this.ControlId = controlId;
84 this.RenderSize = renderSize;
85 this.ViewstateSize = viewstateSize;
88 this.ControlstateSize = controlstateSize;
93 class NameValueTraceData
98 public NameValueTraceData (string name, string value)
105 internal class TraceData {
112 Queue <InfoTraceData> info;
113 Queue <ControlTraceData> control_data;
114 Queue <NameValueTraceData> cookie_data;
115 Queue <NameValueTraceData> header_data;
116 Queue <NameValueTraceData> servervar_data;
123 Queue servervar_data;
124 //DataTable viewstate_data;
129 DateTime request_time;
130 Encoding request_encoding;
131 Encoding response_encoding;
135 TraceMode _traceMode = HttpRuntime.TraceManager.TraceMode;
140 info = new Queue <InfoTraceData> ();
141 control_data = new Queue <ControlTraceData> ();
142 cookie_data = new Queue <NameValueTraceData> ();
143 header_data = new Queue <NameValueTraceData> ();
144 servervar_data = new Queue <NameValueTraceData> ();
147 control_data = new Queue ();
148 cookie_data = new Queue ();
149 header_data = new Queue ();
150 servervar_data = new Queue ();
154 viewstate_data = new DataTable ();
155 viewstate_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
156 viewstate_data.Columns.Add (new DataColumn ("Data", typeof (string)));
159 is_first_time = true;
162 public TraceMode TraceMode {
163 get { return _traceMode; }
164 set { _traceMode = value; }
167 public string RequestPath {
168 get { return request_path; }
169 set { request_path = value; }
172 public string SessionID {
173 get { return session_id; }
174 set { session_id = value; }
177 public DateTime RequestTime {
178 get { return request_time; }
179 set { request_time = value; }
182 public Encoding RequestEncoding {
183 get { return request_encoding; }
184 set { request_encoding = value; }
187 public Encoding ResponseEncoding {
188 get { return response_encoding; }
189 set { response_encoding = value; }
192 public string RequestType {
193 get { return request_type; }
194 set { request_type = value; }
197 public int StatusCode {
198 get { return status_code; }
199 set { status_code = value; }
202 public void Write (string category, string msg, Exception error, bool Warning)
205 double time_from_last;
210 is_first_time = false;
211 first_time = DateTime.Now;
214 time = (DateTime.Now - first_time).TotalSeconds;
215 time_from_last = time - prev_time;
220 new InfoTraceData (category,
222 (error != null ? error.ToString () : null),
228 static string HtmlEncode (string s)
233 string res = HttpUtility.HtmlEncode (s);
234 res = res.Replace ("\n", "<br>");
235 return res.Replace (" ", " ");
239 public void AddControlTree (Page page, Hashtable ctrl_vs, Hashtable ctrl_cs, Hashtable sizes)
241 public void AddControlTree (Page page, Hashtable ctrl_vs, Hashtable sizes)
245 this.ctrl_vs = ctrl_vs;
248 this.ctrl_cs = ctrl_cs;
250 AddControl (page, 0);
255 void AddControl (Control c, int control_pos)
257 control_data.Enqueue (
258 new ControlTraceData (
262 GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null),
264 GetViewStateSize (c, (ctrl_cs != null) ? ctrl_cs [c] : null),
268 if (c.HasControls ()) {
269 foreach (Control child in c.Controls)
270 AddControl (child, control_pos + 1);
274 int GetRenderSize (Control ctrl)
279 object s = sizes [ctrl];
280 return s == null ? 0 : (int) s;
283 static int GetViewStateSize (Control ctrl, object vs)
288 StringWriter sr = new StringWriter ();
289 LosFormatter fmt = new LosFormatter ();
290 fmt.Serialize (sr, vs);
291 return sr.GetStringBuilder ().Length;
294 public void AddCookie (string name, string value)
296 cookie_data.Enqueue (new NameValueTraceData (name, value));
299 public void AddHeader (string name, string value)
301 header_data.Enqueue (new NameValueTraceData (name, value));
304 public void AddServerVar (string name, string value)
306 servervar_data.Enqueue (new NameValueTraceData (name, value));
309 public void Render (HtmlTextWriter output)
311 output.AddAttribute ("id", "__asptrace");
312 output.RenderBeginTag (HtmlTextWriterTag.Div);
314 RenderStyleSheet (output);
316 output.AddAttribute ("class", "tracecontent");
317 output.RenderBeginTag (HtmlTextWriterTag.Span);
319 RenderRequestDetails (output);
320 RenderTraceInfo (output);
321 RenderControlTree (output);
322 RenderCookies (output);
323 RenderHeaders (output);
324 RenderServerVars (output);
326 output.RenderEndTag ();
327 output.RenderEndTag ();
330 void RenderRequestDetails (HtmlTextWriter output)
332 Table table = CreateTable ();
334 table.Rows.Add (AltRow ("Request Details:"));
335 table.Rows.Add (InfoRow2 ("Session Id:", session_id,
336 "Request Type", request_type));
337 table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
338 "State Code:", status_code.ToString ()));
339 table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
340 "Response Encoding:", response_encoding.EncodingName));
341 table.RenderControl (output);
344 void RenderTraceInfo (HtmlTextWriter output)
346 Table table = CreateTable ();
348 table.Rows.Add (AltRow ("Trace Information"));
349 table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
353 IEnumerable<InfoTraceData> enumerable = info;
355 if (TraceMode == TraceMode.SortByCategory) {
356 List<InfoTraceData> list = new List<InfoTraceData> (info);
357 list.Sort (delegate (InfoTraceData x, InfoTraceData y) { return String.Compare (x.Category, y.Category); });
361 foreach (InfoTraceData i in enumerable)
362 RenderTraceInfoRow (table, i, pos++);
364 foreach (object o in info)
365 RenderTraceInfoRow (table, o as InfoTraceData, pos++);
367 table.RenderControl (output);
370 void RenderControlTree (HtmlTextWriter output)
372 Table table = CreateTable ();
374 int page_vs_size = page == null ? 0 : GetViewStateSize (page, page.GetSavedViewState ());
375 table.Rows.Add (AltRow ("Control Tree"));
376 table.Rows.Add (SubHeadRow ("Control Id", "Type",
377 "Render Size Bytes (including children)",
379 "ViewState Size (excluding children)"
381 String.Format ("ViewState Size (total: {0} bytes)(excluding children)",
385 ,"ControlState Size (excluding children)"
391 foreach (ControlTraceData r in control_data)
392 RenderControlTraceDataRow (table, r, pos++);
394 foreach (object o in control_data)
395 RenderControlTraceDataRow (table, o as ControlTraceData, pos++);
397 table.RenderControl (output);
400 void RenderControlTraceDataRow (Table table, ControlTraceData r, int pos)
406 string prefix = String.Empty;
407 for (int i=0; i<depth; i++)
408 prefix += " ";
409 RenderAltRow (table, pos, prefix + r.ControlId,
410 r.Type.ToString (), r.RenderSize.ToString (),
412 r.ViewstateSize.ToString (), r.ControlstateSize.ToString ());
414 r.ViewstateSize.ToString ());
418 void RenderCookies (HtmlTextWriter output)
420 Table table = CreateTable ();
422 table.Rows.Add (AltRow ("Cookies Collection"));
423 table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
427 foreach (NameValueTraceData r in cookie_data)
428 RenderCookieDataRow (table, r, pos++);
430 foreach (object o in cookie_data)
431 RenderCookieDataRow (table, o as NameValueTraceData, pos++);
434 table.RenderControl (output);
437 void RenderCookieDataRow (Table table, NameValueTraceData r, int pos)
442 int length = r.Name.Length + (r.Value == null ? 0 : r.Value.Length);
443 RenderAltRow (table, pos++, r.Name, r.Value, length.ToString ());
446 void RenderHeaders (HtmlTextWriter output)
448 Table table = CreateTable ();
450 table.Rows.Add (AltRow ("Headers Collection"));
451 table.Rows.Add (SubHeadRow ("Name", "Value"));
455 foreach (NameValueTraceData r in header_data)
456 RenderAltRow (table, pos++, r.Name, r.Value);
458 NameValueTraceData r;
459 foreach (object o in header_data) {
460 r = o as NameValueTraceData;
464 RenderAltRow (table, pos++, r.Name, r.Value);
467 table.RenderControl (output);
470 void RenderServerVars (HtmlTextWriter output)
472 Table table = CreateTable ();
474 table.Rows.Add (AltRow ("Server Variables"));
475 table.Rows.Add (SubHeadRow ("Name", "Value"));
479 foreach (NameValueTraceData r in servervar_data)
480 RenderAltRow (table, pos++, r.Name, r.Value);
482 NameValueTraceData r;
483 foreach (object o in servervar_data) {
484 r = o as NameValueTraceData;
488 RenderAltRow (table, pos++, r.Name, r.Value);
491 table.RenderControl (output);
494 internal static TableRow AltRow (string title)
496 TableRow row = new TableRow ();
497 TableHeaderCell header = new TableHeaderCell ();
498 header.CssClass = "alt";
499 header.HorizontalAlign = HorizontalAlign.Left;
500 header.Attributes [" colspan"] = "10";
501 header.Text = "<h3><b>" + title + "</b></h3>";
503 row.Cells.Add (header);
507 void RenderTraceInfoRow (Table table, InfoTraceData i, int pos)
513 open = close = String.Empty;
514 if ((bool) i.IsWarning) {
515 open = "<font color=\"Red\">";
521 if (i.TimeSinceFirst == 0) {
522 t1 = t2 = String.Empty;
526 t1 = i.TimeSinceFirst.ToString ("0.000000");
527 t2 = i.TimeSinceLast.ToString ("0.000000");
530 RenderAltRow (table, pos, open + (string) i.Category + close,
531 open + (string) i.Message + close, t1, t2);
534 internal static TableRow SubHeadRow (params string[] cells)
536 TableRow row = new TableRow ();
537 foreach (string s in cells) {
538 TableHeaderCell cell = new TableHeaderCell ();
540 row.Cells.Add (cell);
543 row.CssClass = "subhead";
544 row.HorizontalAlign = HorizontalAlign.Left;
549 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
551 TableRow row = new TableRow ();
552 foreach (string s in cells) {
553 TableCell cell = new TableCell ();
555 row.Cells.Add (cell);
559 row.CssClass = "alt";
561 table.Rows.Add (row);
565 TableRow InfoRow2 (string title1, string info1, string title2, string info2)
567 TableRow row = new TableRow ();
568 TableHeaderCell header1 = new TableHeaderCell ();
569 TableHeaderCell header2 = new TableHeaderCell ();
570 TableCell cell1 = new TableCell ();
571 TableCell cell2 = new TableCell ();
573 header1.Text = title1;
574 header2.Text = title2;
578 row.Cells.Add (header1);
579 row.Cells.Add (cell1);
580 row.Cells.Add (header2);
581 row.Cells.Add (cell2);
583 row.HorizontalAlign = HorizontalAlign.Left;
588 internal static Table CreateTable ()
590 Table table = new Table ();
592 table.Width = Unit.Percentage (100);
593 table.CellSpacing = 0;
594 table.CellPadding = 0;
599 internal static void RenderStyleSheet (HtmlTextWriter o)
601 o.WriteLine ("<style type=\"text/css\">");
602 o.Write ("span.tracecontent { background-color:white; ");
603 o.WriteLine ("color:black;font: 10pt verdana, arial; }");
604 o.Write ("span.tracecontent table { font: 10pt verdana, ");
605 o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
606 o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
607 o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
608 o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
609 o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
610 o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
611 o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
612 o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
613 o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
614 o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
615 o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
616 o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
617 o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
618 o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
619 o.WriteLine ("color:white; padding:0,5,0,5; }");
620 o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
621 o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
622 o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
623 o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
624 o.WriteLine ("arial;text-decoration:underline;}");
625 o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
626 o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
627 o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
628 o.WriteLine ("span.tracecontent table td { padding-right:20 }");
629 o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
630 o.WriteLine ("</style>");