by Njål

Complete Dump of Asp.net WebRequest

 


Alternate way of dumping logs

 

Here’s a simple but very handy method that lists out all relevant data for a Asp.net web request. Posted Parameters, Query String, Http Headers, Cookies and Session variables are returned – and can be logged out to file/console etc.

Note that this static method can be called anywhere from your code – it does not have to be inside a aspx/ashx etc. The only requirement is that the code must run inside IIS and a web request must be assosiated with the current thread.

 

 

 

public static string DumpRequest()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("");
    sb.AppendLine("-------------  PostedParameters --------------");
    foreach (string key in HttpContext.Current.Request.Form.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.Params[key]);
    }

    sb.AppendLine("");
    sb.AppendLine("-------------  Query String ------------------");
    foreach (string key in HttpContext.Current.Request.QueryString.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.QueryString[key]);
    }

    sb.AppendLine("");
    sb.AppendLine("-------------  Http Headers -------------------");
    foreach (string key in HttpContext.Current.Request.Headers.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.Headers[key]);
    }

    sb.AppendLine("");
    sb.AppendLine("--------------  Cookies -------------------------");
    foreach (string key in HttpContext.Current.Request.Cookies.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.Cookies[key].Value + " - ExpireDate: " +  HttpContext.Current.Request.Cookies[key].Expires);
    }

    sb.AppendLine("");
    sb.AppendLine("---------------  Session ------------------------");
    foreach (string key in HttpContext.Current.Session.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Session[key]);
    }

    return sb.ToString();
}

 

The result looks like this::

-----------------------------------------  PostedParameters -----------------------------------------

-----------------------------------------  Query String ---------------------------------------------
                                username : ted@example.com

-----------------------------------------  Http Headers ---------------------------------------------
                                  Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
                          Accept-Charset : ISO-8859-1,utf-8;q=0.7,*;q=0.3
                         Accept-Encoding : gzip,deflate,sdch
                         Accept-Language : en-US,en;q=0.8
                           Cache-Control : max-age=0
                              Connection : keep-alive
                                  Cookie : timeOffset=-120; gs_u=1746198188:22843:26224:1320393450549; LANG=en; USER=a33f8…
                                    Host : dev.filemail.com
                              User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome..

-----------------------------------------  Cookies --------------------------------------------------
                                  __utma : 155161261.1943841533.1313506820.1324476330.1325070578.60 - ExpireDate: 01.01.0001 00:00:00
                                  __utma : 155161261.1943841533.1313506820.1324476330.1325070578.60 - ExpireDate: 01.01.0001 00:00:00
                                  __utmc : 260895642 - ExpireDate: 01.01.0001 00:00:00
                                  __utmz : 155161261.1313506820.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) - ExpireDate:
                                  __utmz : 155161261.1313506820.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) - ExpireDate:
                       ASP.NET_SessionId : a2xlln05ibzzd2ajywhpizc0 - ExpireDate: 01.01.0001 00:00:00
                                    gs_u : 1746198188:22843:26224:1320393450549 - ExpireDate: 01.01.0001 00:00:00
                                    LANG : en - ExpireDate: 01.01.0001 00:00:00
                              timeOffset : -120 - ExpireDate: 01.01.0001 00:00:00
                                    USER : a33f8a58568f430e8a0d48115ac7b13c - ExpireDate: 01.01.0001 00:00:00

-----------------------------------------  Session --------------------------------------------------
                                       - : -
                              UserObject :