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 object s = sizes [c];
183 r ["RenderSize"] = (s == null) ? 0 : (int) s;
184 r ["ViewstateSize"] = GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null);
186 control_data.Rows.Add (r);
188 if (c.HasControls ()) {
189 foreach (Control child in c.Controls)
190 AddControl (child, control_pos + 1);
194 static int GetViewStateSize (Control ctrl, object vs)
199 StringWriter sr = new StringWriter ();
200 LosFormatter fmt = new LosFormatter ();
201 fmt.Serialize (sr, vs);
202 return sr.GetStringBuilder ().Length;
205 public void AddCookie (string name, string value)
207 DataRow r = cookie_data.NewRow ();
212 cookie_data.Rows.Add (r);
215 public void AddHeader (string name, string value)
217 DataRow r = header_data.NewRow ();
222 header_data.Rows.Add (r);
225 public void AddServerVar (string name, string value)
227 DataRow r = servervar_data.NewRow ();
232 servervar_data.Rows.Add (r);
235 public void Render (HtmlTextWriter output)
237 output.AddAttribute ("id", "__asptrace");
238 output.RenderBeginTag (HtmlTextWriterTag.Div);
240 RenderStyleSheet (output);
242 output.AddAttribute ("class", "tracecontent");
243 output.RenderBeginTag (HtmlTextWriterTag.Span);
245 RenderRequestDetails (output);
246 RenderTraceInfo (output);
247 RenderControlTree (output);
248 RenderCookies (output);
249 RenderHeaders (output);
250 RenderServerVars (output);
252 output.RenderEndTag ();
253 output.RenderEndTag ();
256 private void RenderRequestDetails (HtmlTextWriter output)
258 Table table = CreateTable ();
260 table.Rows.Add (AltRow ("Request Details:"));
261 table.Rows.Add (InfoRow2 ("Session Id:", session_id,
262 "Request Type", request_type));
263 table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
264 "State Code:", status_code.ToString ()));
265 table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
266 "Response Encoding:", response_encoding.EncodingName));
267 table.RenderControl (output);
270 private void RenderTraceInfo (HtmlTextWriter output)
272 Table table = CreateTable ();
274 table.Rows.Add (AltRow ("Trace Information"));
275 table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
278 foreach (DataRow r in info.Rows)
279 RenderTraceInfoRow (table, r, pos++);
281 table.RenderControl (output);
284 private void RenderControlTree (HtmlTextWriter output)
286 Table table = CreateTable ();
288 int page_vs_size = GetViewStateSize (page, page.GetSavedViewState ());
289 table.Rows.Add (AltRow ("Control Tree"));
290 table.Rows.Add (SubHeadRow ("Control Id", "Type",
291 "Render Size Bytes (including children)",
292 String.Format ("View state Size (total: {0} bytes)(excluding children)",
296 foreach (DataRow r in control_data.Rows) {
297 int depth = (int) r ["Depth"];
298 string prefix = String.Empty;
299 for (int i=0; i<depth; i++)
300 prefix += " ";
301 RenderAltRow (table, pos++, prefix + r ["ControlId"],
302 r ["Type"].ToString (), r ["RenderSize"].ToString (),
303 r ["ViewstateSize"].ToString ());
306 table.RenderControl (output);
309 private void RenderCookies (HtmlTextWriter output)
311 Table table = CreateTable ();
313 table.Rows.Add (AltRow ("Cookies Collection"));
314 table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
317 foreach (DataRow r in cookie_data.Rows) {
318 string name = r ["Name"].ToString ();
319 string value = r ["Value"].ToString ();
320 int length = name.Length + (value == null ? 0 : value.Length);
321 RenderAltRow (table, pos++, name, value, length.ToString ());
324 table.RenderControl (output);
327 private void RenderHeaders (HtmlTextWriter output)
329 Table table = CreateTable ();
331 table.Rows.Add (AltRow ("Headers Collection"));
332 table.Rows.Add (SubHeadRow ("Name", "Value"));
335 foreach (DataRow r in header_data.Rows)
336 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
338 table.RenderControl (output);
341 private void RenderServerVars (HtmlTextWriter output)
343 Table table = CreateTable ();
345 table.Rows.Add (AltRow ("Server Variables"));
346 table.Rows.Add (SubHeadRow ("Name", "Value"));
349 foreach (DataRow r in servervar_data.Rows)
350 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
352 table.RenderControl (output);
355 internal static TableRow AltRow (string title)
357 TableRow row = new TableRow ();
358 TableHeaderCell header = new TableHeaderCell ();
359 header.CssClass = "alt";
360 header.HorizontalAlign = HorizontalAlign.Left;
361 header.Attributes [" colspan"] = "10";
362 header.Text = "<h3><b>" + title + "</b></h3>";
364 row.Cells.Add (header);
368 private TableRow RenderTraceInfoRow (Table table, DataRow r, int pos)
371 open = close = String.Empty;
372 if ((bool) r ["IsWarning"]) {
373 open = "<font color=\"Red\">";
377 double t = (double) r ["TimeSinceFirst"];
380 t1 = t2 = String.Empty;
383 t1 = t.ToString ("0.000000");
384 t2 = (t - prev_time).ToString ("0.000000");
388 return RenderAltRow (table, pos, open + (string) r ["Category"] + close,
389 open + (string) r ["Message"] + close, t1, t2);
392 internal static TableRow SubHeadRow (params string[] cells)
394 TableRow row = new TableRow ();
395 foreach (string s in cells) {
396 TableHeaderCell cell = new TableHeaderCell ();
398 row.Cells.Add (cell);
401 row.CssClass = "subhead";
402 row.HorizontalAlign = HorizontalAlign.Left;
407 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
409 TableRow row = new TableRow ();
410 foreach (string s in cells) {
411 TableCell cell = new TableCell ();
413 row.Cells.Add (cell);
417 row.CssClass = "alt";
419 table.Rows.Add (row);
423 private TableRow InfoRow2 (string title1, string info1, string title2, string info2)
425 TableRow row = new TableRow ();
426 TableHeaderCell header1 = new TableHeaderCell ();
427 TableHeaderCell header2 = new TableHeaderCell ();
428 TableCell cell1 = new TableCell ();
429 TableCell cell2 = new TableCell ();
431 header1.Text = title1;
432 header2.Text = title2;
436 row.Cells.Add (header1);
437 row.Cells.Add (cell1);
438 row.Cells.Add (header2);
439 row.Cells.Add (cell2);
441 row.HorizontalAlign = HorizontalAlign.Left;
446 internal static Table CreateTable ()
448 Table table = new Table ();
450 table.Width = Unit.Percentage (100);
451 table.CellSpacing = 0;
452 table.CellPadding = 0;
457 internal static void RenderStyleSheet (HtmlTextWriter o)
459 o.WriteLine ("<style type=\"text/css\">");
460 o.Write ("span.tracecontent { background-color:white; ");
461 o.WriteLine ("color:black;font: 10pt verdana, arial; }");
462 o.Write ("span.tracecontent table { font: 10pt verdana, ");
463 o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
464 o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
465 o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
466 o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
467 o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
468 o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
469 o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
470 o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
471 o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
472 o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
473 o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
474 o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
475 o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
476 o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
477 o.WriteLine ("color:white; padding:0,5,0,5; }");
478 o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
479 o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
480 o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
481 o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
482 o.WriteLine ("arial;text-decoration:underline;}");
483 o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
484 o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
485 o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
486 o.WriteLine ("span.tracecontent table td { padding-right:20 }");
487 o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
488 o.WriteLine ("</style>");