Created
June 26, 2011 10:35
-
-
Save jittuu/1047494 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static string Format(Exception ex) | |
| { | |
| if (ex == null) | |
| throw new ArgumentNullException("ex"); | |
| var baseException = ex.GetBaseException(); | |
| var builder = new StringBuilder(); | |
| builder | |
| .AppendLine() | |
| .AppendFormat("Exception type: {0}", baseException.GetType().FullName).AppendLine() | |
| .AppendFormat("Message: {0}", baseException.Message).AppendLine() | |
| .AppendFormat("StackTrace: {0}", baseException.StackTrace).AppendLine(); | |
| if (HttpContext.Current != null) | |
| { | |
| var request = TryGetRequest(); | |
| if (request != null) | |
| { | |
| builder | |
| .AppendFormat("URL: {0}", request.RawUrl).AppendLine() | |
| .AppendFormat("ContentType : {0}", request.ContentType).AppendLine() | |
| .AppendFormat("HttpMethod: {0}", request.HttpMethod).AppendLine() | |
| .AppendFormat("Headers: {0}", request.Headers.ToFormattedString()).AppendLine() | |
| .AppendFormat("Form: {0}", request.Form.ToFormattedString()).AppendLine() | |
| .AppendFormat("QueryString: {0}", request.QueryString.ToFormattedString()).AppendLine() | |
| .AppendFormat("InputStream: {0}", request.ToFormattedInputString()); | |
| } | |
| } | |
| return builder.ToString(); | |
| } | |
| private static HttpRequest TryGetRequest() | |
| { | |
| try | |
| { | |
| // It might throw the following exception if the Request is not available | |
| // HttpException (0x80004005): Request is not available in this context | |
| return HttpContext.Current.Request; | |
| } | |
| catch (HttpException) | |
| { | |
| return null; | |
| } | |
| } | |
| private static string ToFormattedString(this System.Collections.Specialized.NameValueCollection collection) | |
| { | |
| var sb = new StringBuilder(); | |
| foreach (var key in collection.AllKeys) | |
| { | |
| sb.AppendFormat("\t\t{0}={1}", key, collection[key]).AppendLine(); | |
| } | |
| return sb.ToString(); | |
| } | |
| private static string ToFormattedInputString(this HttpRequest request) | |
| { | |
| var result = ""; | |
| using (var reader = new BinaryReader(request.InputStream)) | |
| { | |
| request.InputStream.Position = 0; | |
| var bytes = reader.ReadBytes((int)request.InputStream.Length); | |
| result = request.ContentEncoding.GetString(bytes); | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment