// Author(s):
// Jackson Harper (jackson@ximian.com)
//
-// (C) 2004 Novell, Inc (http://www.novell.com)
+// (C) 2004-2009 Novell, Inc (http://www.novell.com)
//
//
using System;
using System.Collections;
+using System.Collections.Generic;
+using System.Globalization;
using System.IO;
using System.Text;
-using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
-namespace System.Web {
+namespace System.Web
+{
+ sealed class InfoTraceData
+ {
+ public string Category;
+ public string Message;
+ public string Exception;
+ public double TimeSinceFirst;
+ public double TimeSinceLast;
+ public bool IsWarning;
+
+ public InfoTraceData (string category, string message, string exception, double timeSinceFirst, double timeSinceLast, bool isWarning)
+ {
+ this.Category = category;
+ this.Message = message;
+ this.Exception = exception;
+ this.TimeSinceFirst = timeSinceFirst;
+ this.TimeSinceLast = timeSinceLast;
+ this.IsWarning = isWarning;
+ }
+ }
- internal class TraceData {
+ sealed class ControlTraceData
+ {
+ public string ControlId;
+ public Type Type;
+ public int RenderSize;
+ public int ViewstateSize;
+ public int Depth;
+ public int ControlstateSize;
- private bool is_first_time;
- private DateTime first_time;
- private double prev_time;
-
- private DataTable info;
- private DataTable control_data;
- private DataTable cookie_data;
- private DataTable header_data;
- private DataTable servervar_data;
- //private DataTable viewstate_data;
-
- private string request_path;
- private string session_id;
- private DateTime request_time;
- private Encoding request_encoding;
- private Encoding response_encoding;
- private string request_type;
- private int status_code;
- private Page page;
+ public ControlTraceData (string controlId, Type type, int renderSize, int viewstateSize, int controlstateSize, int depth)
+ {
+ this.ControlId = controlId;
+ this.Type = type;
+ this.RenderSize = renderSize;
+ this.ViewstateSize = viewstateSize;
+ this.Depth = depth;
+ this.ControlstateSize = controlstateSize;
+ }
+ }
+
+ sealed class NameValueTraceData
+ {
+ public string Name;
+ public string Value;
+
+ public NameValueTraceData (string name, string value)
+ {
+ this.Name = name;
+ this.Value = value;
+ }
+ }
+
+ sealed class TraceData
+ {
+ bool is_first_time;
+ DateTime first_time;
+ double prev_time;
+ Queue <InfoTraceData> info;
+ Queue <ControlTraceData> control_data;
+ Queue <NameValueTraceData> cookie_data;
+ Queue <NameValueTraceData> header_data;
+ Queue <NameValueTraceData> servervar_data;
+ Hashtable ctrl_cs;
+ string request_path;
+ string session_id;
+ DateTime request_time;
+ Encoding request_encoding;
+ Encoding response_encoding;
+ string request_type;
+ int status_code;
+ Page page;
+ TraceMode _traceMode = HttpRuntime.TraceManager.TraceMode;
public TraceData ()
{
- info = new DataTable ();
- info.Columns.Add (new DataColumn ("Category", typeof (string)));
- info.Columns.Add (new DataColumn ("Message", typeof (string)));
- info.Columns.Add (new DataColumn ("Exception", typeof (string)));
- info.Columns.Add (new DataColumn ("TimeSinceFirst", typeof (double)));
- info.Columns.Add (new DataColumn ("IsWarning", typeof (bool)));
-
- control_data = new DataTable ();
- control_data.Columns.Add (new DataColumn ("ControlId", typeof (string)));
- control_data.Columns.Add (new DataColumn ("Type", typeof (System.Type)));
- control_data.Columns.Add (new DataColumn ("RenderSize", typeof (int)));
- control_data.Columns.Add (new DataColumn ("ViewstateSize", typeof (int)));
- control_data.Columns.Add (new DataColumn ("Depth", typeof (int)));
-
- cookie_data = new DataTable ();
- cookie_data.Columns.Add (new DataColumn ("Name", typeof (string)));
- cookie_data.Columns.Add (new DataColumn ("Value", typeof (string)));
-
- header_data = new DataTable ();
- header_data.Columns.Add (new DataColumn ("Name", typeof (string)));
- header_data.Columns.Add (new DataColumn ("Value", typeof (string)));
-
- servervar_data = new DataTable ();
- servervar_data.Columns.Add (new DataColumn ("Name", typeof (string)));
- servervar_data.Columns.Add (new DataColumn ("Value", typeof (string)));
+ info = new Queue <InfoTraceData> ();
+ control_data = new Queue <ControlTraceData> ();
+ cookie_data = new Queue <NameValueTraceData> ();
+ header_data = new Queue <NameValueTraceData> ();
+ servervar_data = new Queue <NameValueTraceData> ();
/* TODO
viewstate_data = new DataTable ();
is_first_time = true;
}
+ public TraceMode TraceMode {
+ get { return _traceMode; }
+ set { _traceMode = value; }
+ }
+
public string RequestPath {
get { return request_path; }
set { request_path = value; }
public void Write (string category, string msg, Exception error, bool Warning)
{
double time;
+ double time_from_last;
if (is_first_time) {
time = 0;
+ time_from_last = 0;
+ prev_time = 0;
is_first_time = false;
first_time = DateTime.Now;
- } else
+ }
+ else {
time = (DateTime.Now - first_time).TotalSeconds;
+ time_from_last = time - prev_time;
+ prev_time = time;
+ }
- DataRow r = info.NewRow ();
- r ["Category"] = category;
- r ["Message"] = HtmlEncode (msg);
- r ["Exception"] = (error != null ? error.ToString () : null);
- r ["TimeSinceFirst"] = time;
- r ["IsWarning"] = Warning;
-
- info.Rows.Add (r);
+ info.Enqueue (
+ new InfoTraceData (category,
+ HtmlEncode (msg),
+ (error != null ? error.ToString () : null),
+ time,
+ time_from_last,
+ Warning));
}
static string HtmlEncode (string s)
return res.Replace (" ", " ");
}
- public void AddControlTree (Page page, Hashtable ctrl_vs, Hashtable sizes)
+ public void AddControlTree (Page page, Hashtable ctrl_vs, Hashtable ctrl_cs, Hashtable sizes)
{
this.page = page;
this.ctrl_vs = ctrl_vs;
this.sizes = sizes;
+ this.ctrl_cs = ctrl_cs;
AddControl (page, 0);
}
Hashtable ctrl_vs;
void AddControl (Control c, int control_pos)
{
- DataRow r = control_data.NewRow ();
- r ["ControlId"] = c.UniqueID;
- r ["Type"] = c.GetType ();
- r ["Depth"] = control_pos;
- r ["RenderSize"] = GetRenderSize (c);
- r ["ViewstateSize"] = GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null);
-
- control_data.Rows.Add (r);
-
+ control_data.Enqueue (
+ new ControlTraceData (
+ c.UniqueID,
+ c.GetType (),
+ GetRenderSize (c),
+ GetViewStateSize (c, (ctrl_vs != null) ? ctrl_vs [c] : null),
+ GetViewStateSize (c, (ctrl_cs != null) ? ctrl_cs [c] : null),
+ control_pos));
+
if (c.HasControls ()) {
foreach (Control child in c.Controls)
AddControl (child, control_pos + 1);
public void AddCookie (string name, string value)
{
- DataRow r = cookie_data.NewRow ();
-
- r ["Name"] = name;
- r ["Value"] = value;
-
- cookie_data.Rows.Add (r);
+ cookie_data.Enqueue (new NameValueTraceData (name, value));
}
public void AddHeader (string name, string value)
{
- DataRow r = header_data.NewRow ();
-
- r ["Name"] = name;
- r ["Value"] = value;
-
- header_data.Rows.Add (r);
+ header_data.Enqueue (new NameValueTraceData (name, value));
}
public void AddServerVar (string name, string value)
{
- DataRow r = servervar_data.NewRow ();
-
- r ["Name"] = name;
- r ["Value"] = value;
-
- servervar_data.Rows.Add (r);
+ servervar_data.Enqueue (new NameValueTraceData (name, value));
}
public void Render (HtmlTextWriter output)
output.RenderEndTag ();
}
- private void RenderRequestDetails (HtmlTextWriter output)
+ void RenderRequestDetails (HtmlTextWriter output)
{
Table table = CreateTable ();
table.RenderControl (output);
}
- private void RenderTraceInfo (HtmlTextWriter output)
+ void RenderTraceInfo (HtmlTextWriter output)
{
Table table = CreateTable ();
table.Rows.Add (SubHeadRow ("Category", "Message", "From First(s)", "From Lasts(s)"));
int pos = 0;
- foreach (DataRow r in info.Rows)
- RenderTraceInfoRow (table, r, pos++);
-
+ IEnumerable<InfoTraceData> enumerable = info;
+
+ if (TraceMode == TraceMode.SortByCategory) {
+ List<InfoTraceData> list = new List<InfoTraceData> (info);
+ list.Sort (delegate (InfoTraceData x, InfoTraceData y) { return String.Compare (x.Category, y.Category, StringComparison.Ordinal); });
+ enumerable = list;
+ }
+
+ foreach (InfoTraceData i in enumerable)
+ RenderTraceInfoRow (table, i, pos++);
table.RenderControl (output);
}
- private void RenderControlTree (HtmlTextWriter output)
+ void RenderControlTree (HtmlTextWriter output)
{
Table table = CreateTable ();
- int page_vs_size = GetViewStateSize (page, page.GetSavedViewState ());
+ int page_vs_size = page == null ? 0 : GetViewStateSize (page, page.GetSavedViewState ());
table.Rows.Add (AltRow ("Control Tree"));
table.Rows.Add (SubHeadRow ("Control Id", "Type",
"Render Size Bytes (including children)",
- String.Format ("View state Size (total: {0} bytes)(excluding children)",
- page_vs_size)));
+ String.Format ("ViewState Size (total: {0} bytes)(excluding children)",
+ page_vs_size)
+ ,"ControlState Size (excluding children)"
+ ));
int pos = 0;
- foreach (DataRow r in control_data.Rows) {
- int depth = (int) r ["Depth"];
- string prefix = String.Empty;
- for (int i=0; i<depth; i++)
- prefix += " ";
- RenderAltRow (table, pos++, prefix + r ["ControlId"],
- r ["Type"].ToString (), r ["RenderSize"].ToString (),
- r ["ViewstateSize"].ToString ());
- }
-
+ foreach (ControlTraceData r in control_data)
+ RenderControlTraceDataRow (table, r, pos++);
table.RenderControl (output);
}
- private void RenderCookies (HtmlTextWriter output)
+ void RenderControlTraceDataRow (Table table, ControlTraceData r, int pos)
+ {
+ if (r == null)
+ return;
+
+ int depth = r.Depth;
+ string prefix = String.Empty;
+ for (int i=0; i<depth; i++)
+ prefix += " ";
+ RenderAltRow (table, pos, prefix + r.ControlId,
+ r.Type.ToString (), r.RenderSize.ToString (),
+ r.ViewstateSize.ToString (), r.ControlstateSize.ToString ());
+ }
+
+ void RenderCookies (HtmlTextWriter output)
{
Table table = CreateTable ();
table.Rows.Add (SubHeadRow ("Name", "Value", "Size"));
int pos = 0;
- foreach (DataRow r in cookie_data.Rows) {
- string name = r ["Name"].ToString ();
- string value = r ["Value"].ToString ();
- int length = name.Length + (value == null ? 0 : value.Length);
- RenderAltRow (table, pos++, name, value, length.ToString ());
- }
+ foreach (NameValueTraceData r in cookie_data)
+ RenderCookieDataRow (table, r, pos++);
table.RenderControl (output);
}
+
+ void RenderCookieDataRow (Table table, NameValueTraceData r, int pos)
+ {
+ if (r == null)
+ return;
+
+ int length = r.Name.Length + (r.Value == null ? 0 : r.Value.Length);
+ RenderAltRow (table, pos++, r.Name, r.Value, length.ToString ());
+ }
- private void RenderHeaders (HtmlTextWriter output)
+ void RenderHeaders (HtmlTextWriter output)
{
Table table = CreateTable ();
table.Rows.Add (SubHeadRow ("Name", "Value"));
int pos = 0;
- foreach (DataRow r in header_data.Rows)
- RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
-
+ foreach (NameValueTraceData r in header_data)
+ RenderAltRow (table, pos++, r.Name, r.Value);
table.RenderControl (output);
}
- private void RenderServerVars (HtmlTextWriter output)
+ void RenderServerVars (HtmlTextWriter output)
{
Table table = CreateTable ();
table.Rows.Add (SubHeadRow ("Name", "Value"));
int pos = 0;
- foreach (DataRow r in servervar_data.Rows)
- RenderAltRow (table, pos++, r ["Name"].ToString (), r ["Value"].ToString ());
-
+ foreach (NameValueTraceData r in servervar_data)
+ RenderAltRow (table, pos++, r.Name, r.Value);
table.RenderControl (output);
}
-
+
internal static TableRow AltRow (string title)
{
TableRow row = new TableRow ();
return row;
}
- private TableRow RenderTraceInfoRow (Table table, DataRow r, int pos)
+ void RenderTraceInfoRow (Table table, InfoTraceData i, int pos)
{
+ if (i == null)
+ return;
+
string open, close;
open = close = String.Empty;
- if ((bool) r ["IsWarning"]) {
- open = "<font color=\"Red\">";
- close = "</font>";
+ if ((bool) i.IsWarning) {
+ open = "<span style=\"color:red\">";
+ close = "</span>";
}
-
- double t = (double) r ["TimeSinceFirst"];
+
string t1, t2;
- if (t == 0) {
+ if (i.TimeSinceFirst == 0) {
t1 = t2 = String.Empty;
- prev_time = 0;
- } else {
- t1 = t.ToString ("0.000000");
- t2 = (t - prev_time).ToString ("0.000000");
- prev_time = t;
+ } else
+ {
+ t1 = i.TimeSinceFirst.ToString ("0.000000");
+ if (i.TimeSinceLast >= 0.1)
+ t2 = "<span style=\"color:red;font-weight:bold\">" + i.TimeSinceLast.ToString ("0.000000") + "</span>";
+ else
+ t2 = i.TimeSinceLast.ToString ("0.000000");
}
- return RenderAltRow (table, pos, open + (string) r ["Category"] + close,
- open + (string) r ["Message"] + close, t1, t2);
+ RenderAltRow (table, pos, open + (string) i.Category + close,
+ open + (string) i.Message + close, t1, t2);
}
internal static TableRow SubHeadRow (params string[] cells)
return row;
}
- private TableRow InfoRow2 (string title1, string info1, string title2, string info2)
+ TableRow InfoRow2 (string title1, string info1, string title2, string info2)
{
TableRow row = new TableRow ();
TableHeaderCell header1 = new TableHeaderCell ();