2 // System.Web.TraceData
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2004-2009 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;
34 using System.Collections.Generic;
35 using System.Globalization;
39 using System.Web.UI.WebControls;
43 sealed class InfoTraceData
45 public string Category;
46 public string Message;
47 public string Exception;
48 public double TimeSinceFirst;
49 public double TimeSinceLast;
50 public bool IsWarning;
52 public InfoTraceData (string category, string message, string exception, double timeSinceFirst, double timeSinceLast, bool isWarning)
54 this.Category = category;
55 this.Message = message;
56 this.Exception = exception;
57 this.TimeSinceFirst = timeSinceFirst;
58 this.TimeSinceLast = timeSinceLast;
59 this.IsWarning = isWarning;
63 sealed class ControlTraceData
65 public string ControlId;
67 public int RenderSize;
68 public int ViewstateSize;
70 public int ControlstateSize;
72 public ControlTraceData (string controlId, Type type, int renderSize, int viewstateSize, int controlstateSize, int depth)
74 this.ControlId = controlId;
76 this.RenderSize = renderSize;
77 this.ViewstateSize = viewstateSize;
79 this.ControlstateSize = controlstateSize;
83 sealed class NameValueTraceData
88 public NameValueTraceData (string name, string value)
95 sealed class TraceData
100 Queue <InfoTraceData> info;
101 Queue <ControlTraceData> control_data;
102 Queue <NameValueTraceData> cookie_data;
103 Queue <NameValueTraceData> header_data;
104 Queue <NameValueTraceData> servervar_data;
108 DateTime request_time;
109 Encoding request_encoding;
110 Encoding response_encoding;
114 TraceMode _traceMode = HttpRuntime.TraceManager.TraceMode;
118 info = new Queue <InfoTraceData> ();
119 control_data = new Queue <ControlTraceData> ();
120 cookie_data = new Queue <NameValueTraceData> ();
121 header_data = new Queue <NameValueTraceData> ();
122 servervar_data = new Queue <NameValueTraceData> ();
125 viewstate_data = new DataTable ();
126 viewstate_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
127 viewstate_data.Columns.Add (new DataColumn ("Data", typeof (string)));
130 is_first_time = true;
133 public TraceMode TraceMode {
134 get { return _traceMode; }
135 set { _traceMode = value; }
138 public string RequestPath {
139 get { return request_path; }
140 set { request_path = value; }
143 public string SessionID {
144 get { return session_id; }
145 set { session_id = value; }
148 public DateTime RequestTime {
149 get { return request_time; }
150 set { request_time = value; }
153 public Encoding RequestEncoding {
154 get { return request_encoding; }
155 set { request_encoding = value; }
158 public Encoding ResponseEncoding {
159 get { return response_encoding; }
160 set { response_encoding = value; }
163 public string RequestType {
164 get { return request_type; }
165 set { request_type = value; }
168 public int StatusCode {
169 get { return status_code; }
170 set { status_code = value; }
173 public void Write (string category, string msg, Exception error, bool Warning)
176 double time_from_last;
181 is_first_time = false;
182 first_time = DateTime.Now;
185 time = (DateTime.Now - first_time).TotalSeconds;
186 time_from_last = time - prev_time;
191 new InfoTraceData (category,
193 (error != null ? error.ToString () : null),
199 static string HtmlEncode (string s)
204 string res = HttpUtility.HtmlEncode (s);
205 res = res.Replace ("\n", "<br>");
206 return res.Replace (" ", " ");
209 public void AddControlTree (Page page, Hashtable ctrl_vs, Hashtable ctrl_cs, Hashtable sizes)
212 this.ctrl_vs = ctrl_vs;
214 this.ctrl_cs = ctrl_cs;
215 AddControl (page, 0);
220 void AddControl (Control c, int control_pos)
222 control_data.Enqueue (
223 new ControlTraceData (
227 GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null),
228 GetViewStateSize (c, (ctrl_cs != null) ? ctrl_cs [c] : null),
231 if (c.HasControls ()) {
232 foreach (Control child in c.Controls)
233 AddControl (child, control_pos + 1);
237 int GetRenderSize (Control ctrl)
242 object s = sizes [ctrl];
243 return s == null ? 0 : (int) s;
246 static int GetViewStateSize (Control ctrl, object vs)
251 StringWriter sr = new StringWriter ();
252 LosFormatter fmt = new LosFormatter ();
253 fmt.Serialize (sr, vs);
254 return sr.GetStringBuilder ().Length;
257 public void AddCookie (string name, string value)
259 cookie_data.Enqueue (new NameValueTraceData (name, value));
262 public void AddHeader (string name, string value)
264 header_data.Enqueue (new NameValueTraceData (name, value));
267 public void AddServerVar (string name, string value)
269 servervar_data.Enqueue (new NameValueTraceData (name, value));
272 public void Render (HtmlTextWriter output)
274 output.AddAttribute ("id", "__asptrace");
275 output.RenderBeginTag (HtmlTextWriterTag.Div);
277 RenderStyleSheet (output);
279 output.AddAttribute ("class", "tracecontent");
280 output.RenderBeginTag (HtmlTextWriterTag.Span);
282 RenderRequestDetails (output);
283 RenderTraceInfo (output);
284 RenderControlTree (output);
285 RenderCookies (output);
286 RenderHeaders (output);
287 RenderServerVars (output);
289 output.RenderEndTag ();
290 output.RenderEndTag ();
293 void RenderRequestDetails (HtmlTextWriter output)
295 Table table = CreateTable ();
297 table.Rows.Add (AltRow ("Request Details:"));
298 table.Rows.Add (InfoRow2 ("Session Id:", session_id,
299 "Request Type", request_type));
300 table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
301 "State Code:", status_code.ToString ()));
302 table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
303 "Response Encoding:", response_encoding.EncodingName));
304 table.RenderControl (output);
307 void RenderTraceInfo (HtmlTextWriter output)
309 Table table = CreateTable ();
311 table.Rows.Add (AltRow ("Trace Information"));
312 table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
315 IEnumerable<InfoTraceData> enumerable = info;
317 if (TraceMode == TraceMode.SortByCategory) {
318 List<InfoTraceData> list = new List<InfoTraceData> (info);
319 list.Sort (delegate (InfoTraceData x, InfoTraceData y) { return String.Compare (x.Category, y.Category, StringComparison.Ordinal); });
323 foreach (InfoTraceData i in enumerable)
324 RenderTraceInfoRow (table, i, pos++);
325 table.RenderControl (output);
328 void RenderControlTree (HtmlTextWriter output)
330 Table table = CreateTable ();
332 int page_vs_size = page == null ? 0 : GetViewStateSize (page, page.GetSavedViewState ());
333 table.Rows.Add (AltRow ("Control Tree"));
334 table.Rows.Add (SubHeadRow ("Control Id", "Type",
335 "Render Size Bytes (including children)",
336 String.Format ("ViewState Size (total: {0} bytes)(excluding children)",
338 ,"ControlState Size (excluding children)"
342 foreach (ControlTraceData r in control_data)
343 RenderControlTraceDataRow (table, r, pos++);
344 table.RenderControl (output);
347 void RenderControlTraceDataRow (Table table, ControlTraceData r, int pos)
353 string prefix = String.Empty;
354 for (int i=0; i<depth; i++)
355 prefix += " ";
356 RenderAltRow (table, pos, prefix + r.ControlId,
357 r.Type.ToString (), r.RenderSize.ToString (),
358 r.ViewstateSize.ToString (), r.ControlstateSize.ToString ());
361 void RenderCookies (HtmlTextWriter output)
363 Table table = CreateTable ();
365 table.Rows.Add (AltRow ("Cookies Collection"));
366 table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
369 foreach (NameValueTraceData r in cookie_data)
370 RenderCookieDataRow (table, r, pos++);
372 table.RenderControl (output);
375 void RenderCookieDataRow (Table table, NameValueTraceData r, int pos)
380 int length = r.Name.Length + (r.Value == null ? 0 : r.Value.Length);
381 RenderAltRow (table, pos++, r.Name, r.Value, length.ToString ());
384 void RenderHeaders (HtmlTextWriter output)
386 Table table = CreateTable ();
388 table.Rows.Add (AltRow ("Headers Collection"));
389 table.Rows.Add (SubHeadRow ("Name", "Value"));
392 foreach (NameValueTraceData r in header_data)
393 RenderAltRow (table, pos++, r.Name, r.Value);
394 table.RenderControl (output);
397 void RenderServerVars (HtmlTextWriter output)
399 Table table = CreateTable ();
401 table.Rows.Add (AltRow ("Server Variables"));
402 table.Rows.Add (SubHeadRow ("Name", "Value"));
405 foreach (NameValueTraceData r in servervar_data)
406 RenderAltRow (table, pos++, r.Name, r.Value);
407 table.RenderControl (output);
410 internal static TableRow AltRow (string title)
412 TableRow row = new TableRow ();
413 TableHeaderCell header = new TableHeaderCell ();
414 header.CssClass = "alt";
415 header.HorizontalAlign = HorizontalAlign.Left;
416 header.Attributes [" colspan"] = "10";
417 header.Text = "<h3><b>" + title + "</b></h3>";
419 row.Cells.Add (header);
423 void RenderTraceInfoRow (Table table, InfoTraceData i, int pos)
429 open = close = String.Empty;
430 if ((bool) i.IsWarning) {
431 open = "<span style=\"color:red\">";
436 if (i.TimeSinceFirst == 0) {
437 t1 = t2 = String.Empty;
440 t1 = i.TimeSinceFirst.ToString ("0.000000");
441 if (i.TimeSinceLast >= 0.1)
442 t2 = "<span style=\"color:red;font-weight:bold\">" + i.TimeSinceLast.ToString ("0.000000") + "</span>";
444 t2 = i.TimeSinceLast.ToString ("0.000000");
447 RenderAltRow (table, pos, open + (string) i.Category + close,
448 open + (string) i.Message + close, t1, t2);
451 internal static TableRow SubHeadRow (params string[] cells)
453 TableRow row = new TableRow ();
454 foreach (string s in cells) {
455 TableHeaderCell cell = new TableHeaderCell ();
457 row.Cells.Add (cell);
460 row.CssClass = "subhead";
461 row.HorizontalAlign = HorizontalAlign.Left;
466 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
468 TableRow row = new TableRow ();
469 foreach (string s in cells) {
470 TableCell cell = new TableCell ();
472 row.Cells.Add (cell);
476 row.CssClass = "alt";
478 table.Rows.Add (row);
482 TableRow InfoRow2 (string title1, string info1, string title2, string info2)
484 TableRow row = new TableRow ();
485 TableHeaderCell header1 = new TableHeaderCell ();
486 TableHeaderCell header2 = new TableHeaderCell ();
487 TableCell cell1 = new TableCell ();
488 TableCell cell2 = new TableCell ();
490 header1.Text = title1;
491 header2.Text = title2;
495 row.Cells.Add (header1);
496 row.Cells.Add (cell1);
497 row.Cells.Add (header2);
498 row.Cells.Add (cell2);
500 row.HorizontalAlign = HorizontalAlign.Left;
505 internal static Table CreateTable ()
507 Table table = new Table ();
509 table.Width = Unit.Percentage (100);
510 table.CellSpacing = 0;
511 table.CellPadding = 0;
516 internal static void RenderStyleSheet (HtmlTextWriter o)
518 o.WriteLine ("<style type=\"text/css\">");
519 o.Write ("span.tracecontent { background-color:white; ");
520 o.WriteLine ("color:black;font: 10pt verdana, arial; }");
521 o.Write ("span.tracecontent table { font: 10pt verdana, ");
522 o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
523 o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
524 o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
525 o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
526 o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
527 o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
528 o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
529 o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
530 o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
531 o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
532 o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
533 o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
534 o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
535 o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
536 o.WriteLine ("color:white; padding:0,5,0,5; }");
537 o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
538 o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
539 o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
540 o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
541 o.WriteLine ("arial;text-decoration:underline;}");
542 o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
543 o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
544 o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
545 o.WriteLine ("span.tracecontent table td { padding-right:20 }");
546 o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
547 o.WriteLine ("</style>");