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.
36 using System.Web.UI.WebControls;
38 namespace System.Web {
40 internal class TraceData {
42 private bool is_first_time;
43 private DateTime first_time;
44 private double prev_time;
46 private DataTable info;
47 private DataTable control_data;
48 private DataTable cookie_data;
49 private DataTable header_data;
50 private DataTable servervar_data;
52 private string request_path;
53 private string session_id;
54 private DateTime request_time;
55 private Encoding request_encoding;
56 private Encoding response_encoding;
57 private string request_type;
58 private int status_code;
62 info = new DataTable ();
63 info.Columns.Add (new DataColumn ("Category", typeof (string)));
64 info.Columns.Add (new DataColumn ("Message", typeof (string)));
65 info.Columns.Add (new DataColumn ("Exception", typeof (string)));
66 info.Columns.Add (new DataColumn ("TimeSinceFirst", typeof (double)));
67 info.Columns.Add (new DataColumn ("IsWarning", typeof (bool)));
69 control_data = new DataTable ();
70 control_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
71 control_data.Columns.Add (new DataColumn ("Type", typeof (System.Type)));
72 control_data.Columns.Add (new DataColumn ("RenderSize", typeof (int)));
73 control_data.Columns.Add (new DataColumn ("ViewstateSize", typeof (int)));
74 control_data.Columns.Add (new DataColumn ("Depth", typeof (int)));
76 cookie_data = new DataTable ();
77 cookie_data.Columns.Add (new DataColumn ("Name", typeof (string)));
78 cookie_data.Columns.Add (new DataColumn ("Value", typeof (string)));
80 header_data = new DataTable ();
81 header_data.Columns.Add (new DataColumn ("Name", typeof (string)));
82 header_data.Columns.Add (new DataColumn ("Value", typeof (string)));
84 servervar_data = new DataTable ();
85 servervar_data.Columns.Add (new DataColumn ("Name", typeof (string)));
86 servervar_data.Columns.Add (new DataColumn ("Value", typeof (string)));
91 public string RequestPath {
92 get { return request_path; }
93 set { request_path = value; }
96 public string SessionID {
97 get { return session_id; }
98 set { session_id = value; }
101 public DateTime RequestTime {
102 get { return request_time; }
103 set { request_time = value; }
106 public Encoding RequestEncoding {
107 get { return request_encoding; }
108 set { request_encoding = value; }
111 public Encoding ResponseEncoding {
112 get { return response_encoding; }
113 set { response_encoding = value; }
116 public string RequestType {
117 get { return request_type; }
118 set { request_type = value; }
121 public int StatusCode {
122 get { return status_code; }
123 set { status_code = value; }
126 public void Write (string category, string msg, Exception error, bool Warning)
131 is_first_time = false;
132 first_time = DateTime.Now;
134 time = (DateTime.Now - first_time).TotalSeconds;
136 DataRow r = info.NewRow ();
137 r ["Category"] = category;
138 r ["Message"] = HtmlEncode (msg);
139 r ["Exception"] = (error != null ? error.ToString () : null);
140 r ["TimeSinceFirst"] = time;
141 r ["IsWarning"] = Warning;
146 static string HtmlEncode (string s)
151 string res = HttpUtility.HtmlEncode (s);
152 res = res.Replace ("\n", "<br>");
153 return res.Replace (" ", " ");
156 public void AddControlTree (Page page)
158 AddControl (page, 0);
161 private void AddControl (Control c, int control_pos)
163 DataRow r = control_data.NewRow ();
164 r ["ControlId"] = c.UniqueID;
165 r ["Type"] = c.GetType ();
166 r ["Depth"] = control_pos;
168 control_data.Rows.Add (r);
170 foreach (Control child in c.Controls)
171 AddControl (child, control_pos + 1);
174 public void AddCookie (string name, string value)
176 DataRow r = cookie_data.NewRow ();
181 cookie_data.Rows.Add (r);
184 public void AddHeader (string name, string value)
186 DataRow r = header_data.NewRow ();
191 header_data.Rows.Add (r);
194 public void AddServerVar (string name, string value)
196 DataRow r = servervar_data.NewRow ();
201 servervar_data.Rows.Add (r);
204 public void Render (HtmlTextWriter output)
206 output.AddAttribute ("id", "__asptrace");
207 output.RenderBeginTag (HtmlTextWriterTag.Div);
209 RenderStyleSheet (output);
211 output.AddAttribute ("class", "tracecontent");
212 output.RenderBeginTag (HtmlTextWriterTag.Span);
214 RenderRequestDetails (output);
215 RenderTraceInfo (output);
216 RenderControlTree (output);
217 RenderCookies (output);
218 RenderHeaders (output);
219 RenderServerVars (output);
221 output.RenderEndTag ();
222 output.RenderEndTag ();
225 private void RenderRequestDetails (HtmlTextWriter output)
227 Table table = CreateTable ();
229 table.Rows.Add (AltRow ("Request Details:"));
230 table.Rows.Add (InfoRow2 ("Session Id:", session_id,
231 "Request Type", request_type));
232 table.Rows.Add (InfoRow2 ("Time of Request:", request_time.ToString (),
233 "State Code:", status_code.ToString ()));
234 table.Rows.Add (InfoRow2 ("Request Encoding:", request_encoding.EncodingName,
235 "Response Encoding:", response_encoding.EncodingName));
236 table.RenderControl (output);
239 private void RenderTraceInfo (HtmlTextWriter output)
241 Table table = CreateTable ();
243 table.Rows.Add (AltRow ("Trace Information"));
244 table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
247 foreach (DataRow r in info.Rows)
248 RenderTraceInfoRow (table, r, pos++);
250 table.RenderControl (output);
253 private void RenderControlTree (HtmlTextWriter output)
255 Table table = CreateTable ();
257 table.Rows.Add (AltRow ("Control Tree"));
258 table.Rows.Add (SubHeadRow ("Control Id", "Type",
259 "Render Size Bytes (including children)",
260 "View state Size Bytes (excluding children)"));
263 foreach (DataRow r in control_data.Rows) {
264 int depth = (int) r ["Depth"];
265 string prefix = String.Empty;
266 for (int i=0; i<depth; i++)
267 prefix += " ";
268 RenderAltRow (table, pos++, prefix + r ["ControlId"],
269 r ["Type"].ToString (), " ", " ");
272 table.RenderControl (output);
275 private void RenderCookies (HtmlTextWriter output)
277 Table table = CreateTable ();
279 table.Rows.Add (AltRow ("Cookies Collection"));
280 table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
283 foreach (DataRow r in cookie_data.Rows) {
284 string name = r ["Name"].ToString ();
285 string value = r ["Value"].ToString ();
286 int length = name.Length + (value == null ? 0 : value.Length);
287 RenderAltRow (table, pos++, name, value, length.ToString ());
290 table.RenderControl (output);
293 private void RenderHeaders (HtmlTextWriter output)
295 Table table = CreateTable ();
297 table.Rows.Add (AltRow ("Headers Collection"));
298 table.Rows.Add (SubHeadRow ("Name", "Value"));
301 foreach (DataRow r in header_data.Rows)
302 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
304 table.RenderControl (output);
307 private void RenderServerVars (HtmlTextWriter output)
309 Table table = CreateTable ();
311 table.Rows.Add (AltRow ("Server Variables"));
312 table.Rows.Add (SubHeadRow ("Name", "Value"));
315 foreach (DataRow r in servervar_data.Rows)
316 RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
318 table.RenderControl (output);
321 internal static TableRow AltRow (string title)
323 TableRow row = new TableRow ();
324 TableHeaderCell header = new TableHeaderCell ();
325 header.CssClass = "alt";
326 header.HorizontalAlign = HorizontalAlign.Left;
327 header.Attributes [" colspan"] = "10";
328 header.Text = "<h3><b>" + title + "</b></h3>";
330 row.Cells.Add (header);
334 private TableRow RenderTraceInfoRow (Table table, DataRow r, int pos)
337 open = close = String.Empty;
338 if ((bool) r ["IsWarning"]) {
339 open = "<font color=\"Red\">";
343 double t = (double) r ["TimeSinceFirst"];
346 t1 = t2 = String.Empty;
349 t1 = t.ToString ("0.000000");
350 t2 = (t - prev_time).ToString ("0.000000");
354 return RenderAltRow (table, pos, open + (string) r ["Category"] + close,
355 open + (string) r ["Message"] + close, t1, t2);
358 internal static TableRow SubHeadRow (params string[] cells)
360 TableRow row = new TableRow ();
361 foreach (string s in cells) {
362 TableHeaderCell cell = new TableHeaderCell ();
364 row.Cells.Add (cell);
367 row.CssClass = "subhead";
368 row.HorizontalAlign = HorizontalAlign.Left;
373 internal static TableRow RenderAltRow (Table table, int pos, params string[] cells)
375 TableRow row = new TableRow ();
376 foreach (string s in cells) {
377 TableCell cell = new TableCell ();
379 row.Cells.Add (cell);
383 row.CssClass = "alt";
385 table.Rows.Add (row);
389 private TableRow InfoRow2 (string title1, string info1, string title2, string info2)
391 TableRow row = new TableRow ();
392 TableHeaderCell header1 = new TableHeaderCell ();
393 TableHeaderCell header2 = new TableHeaderCell ();
394 TableCell cell1 = new TableCell ();
395 TableCell cell2 = new TableCell ();
397 header1.Text = title1;
398 header2.Text = title2;
402 row.Cells.Add (header1);
403 row.Cells.Add (cell1);
404 row.Cells.Add (header2);
405 row.Cells.Add (cell2);
407 row.HorizontalAlign = HorizontalAlign.Left;
412 internal static Table CreateTable ()
414 Table table = new Table ();
416 table.Width = Unit.Percentage (100);
417 table.CellSpacing = 0;
418 table.CellPadding = 0;
423 internal static void RenderStyleSheet (HtmlTextWriter o)
425 o.WriteLine ("<style type=\"text/css\">");
426 o.Write ("span.tracecontent { background-color:white; ");
427 o.WriteLine ("color:black;font: 10pt verdana, arial; }");
428 o.Write ("span.tracecontent table { font: 10pt verdana, ");
429 o.WriteLine ("arial; cellspacing:0; cellpadding:0; margin-bottom:25}");
430 o.WriteLine ("span.tracecontent tr.subhead { background-color:cccccc;}");
431 o.WriteLine ("span.tracecontent th { padding:0,3,0,3 }");
432 o.WriteLine ("span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }");
433 o.WriteLine ("span.tracecontent td { padding:0,3,0,3 }");
434 o.WriteLine ("span.tracecontent tr.alt { background-color:eeeeee }");
435 o.WriteLine ("span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}");
436 o.WriteLine ("span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}");
437 o.WriteLine ("span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}");
438 o.WriteLine ("span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }");
439 o.WriteLine ("span.tracecontent a { color:darkblue;text-decoration:none }");
440 o.WriteLine ("span.tracecontent a:hover { color:darkblue;text-decoration:underline; }");
441 o.WriteLine ("span.tracecontent div.outer { width:90%; margin:15,15,15,15}");
442 o.Write ("span.tracecontent table.viewmenu td { background-color:006699; ");
443 o.WriteLine ("color:white; padding:0,5,0,5; }");
444 o.WriteLine ("span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }");
445 o.WriteLine ("span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }");
446 o.WriteLine ("span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }");
447 o.WriteLine ("span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, ");
448 o.WriteLine ("arial;text-decoration:underline;}");
449 o.WriteLine ("span.tracecontent a.link {color:darkblue; text-decoration:underline;}");
450 o.WriteLine ("span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}");
451 o.WriteLine ("span.tracecontent .small { font: 8pt verdana, arial }");
452 o.WriteLine ("span.tracecontent table td { padding-right:20 }");
453 o.WriteLine ("span.tracecontent table td.nopad { padding-right:5 }");
454 o.WriteLine ("</style>");