Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Configuration;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Web;
- using System.Web.Caching;
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Web.SessionState;
- namespace Core.Diagnostics {
- using Configuration;
- using Core.Collections.Specialized;
- using Core.Text;
- using Threading;
- using Web;
- public static class TraceHelper {
- public static void TraceRoutes(RouteData routes) {
- if(routes == null)
- throw new ArgumentNullException("routes");
- Trace.WriteLine("--------------- TRACE_ROUTES:" + HttpContext.Current.Request.Url.OriginalString);
- Trace.IndentLevel += 1;
- foreach(string rkey in routes.Values.Keys) {
- var rval = routes.Values[rkey];
- Trace.WriteLine(@"{0} : ""{1}""".FormatString(rkey,rval.ToStringEmpty()));
- }
- Trace.IndentLevel -= 1;
- Trace.Flush();
- }
- public static void TraceObject(object obj,StringBuilder builder = null) {
- if(obj == null)
- throw new ArgumentNullException("obj");
- Trace.IndentSize = 2;
- Trace.IndentLevel += 1;
- Type t = obj.GetType();
- string indentPrefix = "".PadLeft(Trace.IndentLevel,'\t');
- var dictonary = TypeUtilities.GetPropertyValues(obj);
- Trace.WriteLine("OBJECT_START -> " + t.Name);
- if(builder != null)
- builder.AppendLine(indentPrefix + "OBJECT_START -> " + t.Name);
- foreach(string propname in dictonary.Keys) {
- var propval = dictonary[propname];
- if(propval == null) {
- Trace.WriteLine(@"{0} : {1}".FormatString(propname,"null"));
- if(builder != null)
- builder.AppendLine(indentPrefix + indentPrefix + @"{0} : {1}".FormatString(propname,"null"));
- }
- else {
- if(propval.GetType().Namespace.StartsWith("PinotsPalette") && !propval.GetType().IsEnum) {
- Trace.IndentLevel += 2;
- try {
- Trace.WriteLine(propname + " :");
- if(builder != null)
- builder.AppendLine(indentPrefix + indentPrefix + propname + " :");
- TraceObject(propval,builder);
- }
- catch(Exception error) {
- Trace.WriteLine(error.Message);
- }
- finally {
- Trace.IndentLevel -= 2;
- }
- }
- else {
- Trace.WriteLine(@"{0} : {1}".FormatString(propname,propval ?? "null"));
- if(builder != null)
- builder.AppendLine(indentPrefix + indentPrefix + @"{0} : {1}".FormatString(propname,propval ?? "null"));
- }
- }
- }
- Trace.WriteLine("OBJECT_END -> " + t.Name);
- if(builder != null)
- builder.AppendLine(indentPrefix + "OBJECT_END -> " + t.Name);
- Trace.Flush();
- Trace.IndentLevel -= 1;
- }
- public static StringBuilder GetWebSiteValues(ViewContext viewContext) {
- StringBuilder builder = new StringBuilder(5000);
- HttpContext context = HttpContext.Current;
- if(context == null)
- throw new ArgumentNullException("HttpContext.Current returned null");
- var routes = viewContext.RouteData;
- ModelStateDictionary modelState = viewContext.ViewData.ModelState;
- var request = context.Request;
- var session = context.Session;
- var application = context.Application;
- System.Web.UI.HtmlControls.HtmlTable table = new System.Web.UI.HtmlControls.HtmlTable();
- AppendDataAsTable(RouteValuesToDictionary(routes) as IDictionary,builder,"Routes");
- AppendDataAsTable(ModelStateValuesToDictionary(modelState),builder,"Model State");
- AppendDataAsTable(SessionToDictionary() as IDictionary,builder,"Session Values");
- AppendDataAsTable(ContextToDictionary() as IDictionary,builder,"Http Context Values");
- AppendDataAsTable(ApplicationToDictionary() as IDictionary,builder,"Application Values");
- builder.Append("<p><hr/></p>");
- AppendDataAsTable(request.QueryString,builder,"Request - Query String");
- AppendDataAsTable(request.Params,builder,"Request - All Parameters");
- AppendDataAsTable(request.Form,builder,"Request - Form");
- AppendDataAsTable(request.Headers,builder,"Request - Headers");
- AppendDataAsTable(CookiesToDictionary(request.Cookies),builder,"Request Cookies");
- return builder;
- }
- static void AppendDataAsTable(object data,StringBuilder builder,string name) {
- StringBuilder tbuilder = new StringBuilder();
- if(data is NameValueCollection) {
- var nvc = (data as NameValueCollection).AsDictionary();
- var filtered = nvc.Where(kv => !kv.Value.IsNullOrEmptyTrim());
- DictionaryToHtmlTable.RenderToString(filtered.ToDictionary(d => d.Key,d => d.Value),tbuilder);
- }
- else if(data is Dictionary<string,string>) {
- var filtered = (data as Dictionary<string,string>).Where(kv => !kv.Value.IsNullOrEmptyTrim());
- DictionaryToHtmlTable.RenderToString(filtered.ToDictionary(d => d.Key,d => d.Value),tbuilder);
- }
- else
- return;
- TagBuilder div = new TagBuilder("div");
- TagBuilder h2 = new TagBuilder("h2");
- h2.SetInnerText(name);
- div.InnerHtml = h2.ToString() + tbuilder.ToString();
- builder.Append(div.ToString());
- }
- public static Dictionary<string,string> RouteValuesToDictionary(RouteData routes) {
- if(routes == null)
- throw new ArgumentNullException("routes is null");
- Dictionary<string,string> dict = new Dictionary<string,string>(routes.Values.Count);
- foreach(string rkey in routes.Values.Keys) {
- dict.Add(rkey,routes.Values[rkey].ToStringEmpty());
- }
- return dict;
- }
- public static Dictionary<string,string> ModelStateValuesToDictionary(ModelStateDictionary model) {
- if(model == null)
- throw new ArgumentNullException("model is null");
- Dictionary<string,string> dict = new Dictionary<string,string>(model.Count);
- var sorted = model.OrderByDescending(m => m.Value.Errors.Count > 0);
- foreach(var kp in sorted) {
- string rkey = kp.Key;
- ModelState state = model[rkey];
- if(state == null)
- continue;
- if(state.Value == null)
- continue;
- StringBuilder tbuilder = new StringBuilder();
- tbuilder.Append("<span>");
- object raw = null;
- if(state.Value.RawValue != null) {
- if(state.Value.RawValue is string[]) {
- var arr = state.Value.RawValue as string[];
- foreach(var item in arr) {
- raw = arr.First();
- tbuilder.AppendFormat("{0}",string.Join(";",arr));
- }
- }
- else
- tbuilder.AppendFormat("{0}",state.Value.RawValue);
- }
- if(state.Value.AttemptedValue != null) {
- if(raw.ToStringEmpty() != state.Value.AttemptedValue)
- tbuilder.AppendFormat(" -> Attempted Value: {0}",state.Value.AttemptedValue);
- }
- foreach(ModelError err in state.Errors) {
- tbuilder.AppendFormat("<b style='color:Red;'>Error: {0}</b>",err.ErrorMessage);
- if(err.Exception != null)
- tbuilder.AppendFormat("<b style='color:Red;'>Exception: {0}</b>",err.Exception.ToString().Replace(Environment.NewLine,"<br/>"));
- }
- tbuilder.Append("</span>");
- dict.Add(rkey,tbuilder.ToString());
- }
- return dict;
- }
- public static Dictionary<string,string> CookiesToDictionary(HttpCookieCollection cookies) {
- Dictionary<string,string> dict = new Dictionary<string,string>();
- foreach(string key in cookies.AllKeys) {
- var val = cookies[key];
- StringBuilder builder = new StringBuilder();
- builder.Append("<div>");
- TraceObject(val,builder);
- builder.Append("</div>");
- dict.Add(key,TraceToHtml(builder.ToString()));
- }
- return dict;
- }
- public static Dictionary<string,string> SessionToDictionary() {
- HttpContext context = HttpContext.Current;
- if(context == null)
- throw new ArgumentNullException("HttpContext.Current returned null");
- var session = context.Session;
- Dictionary<string,string> dict = new Dictionary<string,string>(session.Count);
- foreach(string key in session.Keys)
- if(key != null)
- if(!dict.ContainsKey(key.ToString())) {
- var val = session[key.ToString()];
- if(val != null) {
- if(val.GetType().Namespace.StartsWith("PinotsPalette")) {
- StringBuilder tbuilder = new StringBuilder();
- TraceObject(val,tbuilder);
- string html = TraceToHtml(tbuilder.ToString());
- dict.Add(key.ToString(),"<pre></pre><div>" + html + "</div>");
- }
- else
- dict.Add(key.ToString(),session[key.ToString()].ToStringEmpty());
- }
- }
- return dict;
- }
- static string TraceToHtml(string data) {
- data = data.Replace("\t"," ");
- data = data.Replace("OBJECT_START","<b>OBJECT_START</b>");
- data = data.Replace("OBJECT_END","<i><b>OBJECT_END</b></i>");
- data = data.Replace(Environment.NewLine,"<br/>");
- return data;
- }
- public static Dictionary<string,string> ApplicationToDictionary() {
- HttpContext context = HttpContext.Current;
- if(context == null)
- throw new ArgumentNullException("HttpContext.Current returned null");
- var application = context.Application;
- Dictionary<string,string> dict = new Dictionary<string,string>(application.Count);
- foreach(object key in application.Keys)
- if(key != null)
- if(!dict.ContainsKey(key.ToString()))
- if(!application[key.ToString()].ToStringEmpty().IsNullOrEmptyTrim())
- dict.Add(key.ToString(),application[key.ToString()].ToStringEmpty());
- return dict;
- }
- public static Dictionary<string,string> ContextToDictionary() {
- HttpContext context = HttpContext.Current;
- if(context == null)
- throw new ArgumentNullException("HttpContext.Current returned null");
- var items = context.Items;
- Dictionary<string,string> dict = new Dictionary<string,string>(items.Count);
- foreach(object key in items.Keys)
- if(key != null)
- if(!dict.ContainsKey(key.ToString()))
- if(!items[key.ToString()].ToStringEmpty().IsNullOrEmptyTrim())
- dict.Add(key.ToString(),items[key.ToString()].ToStringEmpty());
- return dict;
- }
- }
- public class RuntimeTraceListener:TraceListener {
- public StringBuilder TracedData { get; set; }
- public RuntimeTraceListener() {
- TracedData = new StringBuilder();
- }
- public override void Write(string message) {
- TracedData.Append(message);
- }
- public override void WriteLine(string message) {
- TracedData.AppendLine(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment