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)",
337 "ViewState Size (excluding children)"
339 String.Format ("ViewState Size (total: {0} bytes)(excluding children)",
342 ,"ControlState Size (excluding children)"
346 foreach (ControlTraceData r in control_data)
347 RenderControlTraceDataRow (table, r, pos++);
348 table.RenderControl (output);
351 void RenderControlTraceDataRow (Table table, ControlTraceData r, int pos)
357 string prefix = String.Empty;
358 for (int i=0; i<depth; i++)
359 prefix += " ";
360 RenderAltRow (table, pos, prefix + r.ControlId,
361 r.Type.ToString (), r.RenderSize.ToString (),
362 r.ViewstateSize.ToString (), r.ControlstateSize.ToString ());
365 void RenderCookies (HtmlTextWriter output)
367 Table table = CreateTable ();
369 table.Rows.Add (AltRow ("Cookies Collection"));
370 table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
373 foreach (NameValueTraceData r in cookie_data)
374 RenderCookieDataRow (table, r, pos++);
376 table.RenderControl (output);
379 void RenderCookieDataRow (Table table, NameValueTraceData r, int pos)
384 int length = r.Name.Length + (r.Value == null ? 0 : r.Value.Length);
385 RenderAltRow (table, pos++, r.Name, r.Value, length.ToString ());
388 void RenderHeaders (HtmlTextWriter output)
390 Table table = CreateTable ();
392 table.Rows.Add (AltRow ("Headers Collection"));
393 table.Rows.Add (SubHeadRow ("Name", "Value"));
396 foreach (NameValueTraceData r in header_data)
397 RenderAltRow (table, pos++, r.Name, r.Value);
398 table.RenderControl (output);
401 void RenderServerVars (HtmlTextWriter output)
403 Table table = CreateTable ();
405 table.Rows.Add (AltRow ("Server Variables"));
406 table.Rows.Add (SubHeadRow ("Name", "Value"));
409 foreach (NameValueTraceData r in servervar_data)
410 RenderAltRow (table, pos++, r.Name, r.Value);
411 table.RenderControl (output);
414 internal static TableRow AltRow (string title)
416 TableRow row = new TableRow ();
417 TableHeaderCell header = new TableHeaderCell ();
418 header.CssClass = "alt";
419 header.HorizontalAlign = HorizontalAlign.Left;
420 header.Attributes [" colspan"] = "10";
421 header.Text = "<h3><b>" + title + "</b></h3>";
423 row.Cells.Add (header);
427 void RenderTraceInfoRow (Table table, InfoTraceData i, int pos)
433 open = close = String.Empty;
434 if ((bool) i.IsWarning) {
435 open = "<span style=\"color:red\">";
441 if (i.TimeSinceFirst == 0) {
442 t1 = t2 = String.Empty;
446 t1 = i.TimeSinceFirst.ToString ("0.000000");
447 if (i.TimeSinceLast >= 0.1)
448 t2 = "<span style=\"color:red;font-weight:bold\">" + i.TimeSinceLast.ToString ("0.000000") + "</span>";
450 t2 = i.TimeSinceLast.ToString ("0.000000");
453 RenderAltRow (table, pos, open + (string) i.Category + close,
454 open + (string) i.Message + close, t1, t2);
457 internal static TableRow SubHeadRow (params string[] cells)
459 TableRow row = new TableRow ();
460 foreach (string s in cells) {
461 TableHeaderCell cell = new TableHeaderCell ();
463 row.Cells.Add (cell);
466 row.CssClass = "subhead";
467 row.HorizontalAlign = HorizontalAlign.Left;
472 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
474 TableRow row = new TableRow ();
475 foreach (string s in cells) {
476 TableCell cell = new TableCell ();
478 row.Cells.Add (cell);
482 row.CssClass = "alt";
484 table.Rows.Add (row);
488 TableRow InfoRow2 (string title1, string info1, string title2, string info2)
490 TableRow row = new TableRow ();
491 TableHeaderCell header1 = new TableHeaderCell ();
492 TableHeaderCell header2 = new TableHeaderCell ();
493 TableCell cell1 = new TableCell ();
494 TableCell cell2 = new TableCell ();
496 header1.Text = title1;
497 header2.Text = title2;
501 row.Cells.Add (header1);
502 row.Cells.Add (cell1);
503 row.Cells.Add (header2);
504 row.Cells.Add (cell2);
506 row.HorizontalAlign = HorizontalAlign.Left;
511 internal static Table CreateTable ()
513 Table table = new Table ();
515 table.Width = Unit.Percentage (100);
516 table.CellSpacing = 0;
517 table.CellPadding = 0;
522 internal static void RenderStyleSheet (HtmlTextWriter o)
524 o.WriteLine ("<style type=\"text/css\">");
525 o.Write ("span.tracecontent { background-color:white; ");
526 o.WriteLine ("color:black;font: 10pt verdana, arial; }");
527 o.Write ("span.tracecontent table { font: 10pt verdana, ");
528 o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
529 o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
530 o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
531 o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
532 o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
533 o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
534 o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
535 o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
536 o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
537 o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
538 o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
539 o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
540 o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
541 o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
542 o.WriteLine ("color:white; padding:0,5,0,5; }");
543 o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
544 o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
545 o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
546 o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
547 o.WriteLine ("arial;text-decoration:underline;}");
548 o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
549 o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
550 o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
551 o.WriteLine ("span.tracecontent table td { padding-right:20 }");
552 o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
553 o.WriteLine ("</style>");