"Sometimes, web applications error out," Brian writes via the Submit to The Daily WTF plug-in. "In ASP.NET, a simple web.config entry will redirect all errors to a given location like, errors.aspx. On that page, you can call Server.GetLastError() to get all the error deatils you'd like, including a full stack trace. For more advanced scenarios, you can use the Application_Error event to log errors. "

"Or, you could do what my predecessor did and rewrite the built-in behavior with something woefully inadequate. In this case, he decided to build some kind of XML object to wrap a System.Exception, then forward that error in a URL encoded with Base 64."

protected void Application_Error(object sender, EventArgs e)
{

    ASCIIEncoding encoding = new ASCIIEncoding();
    StringWriter exceptionString = new StringWriter();
    Exception ex = Server.GetLastError();

    if (ex.InnerException != null)
        ex = ex.InnerException;


    string url = HttpContext.Current.Request.Url.AbsoluteUri;

    log.Error("File Request: " + url, ex);


    string queryString = string.Empty;

    //This is a code snippet for serializing Exceptions using LINQ
    XElement root = new XElement("Exception");

    if (ex.Message != null)
    {
        root.Add(new XElement("Message", ex.Message));
    }

    if (ex.StackTrace != null)
    {
        root.Add
        (
            new XElement("StackTrace",
                from frame in ex.StackTrace.Split('\n')
                let prettierFrame = frame.Substring(6).Trim()
                select new XElement("Frame", prettierFrame))
        );
    }

    if (ex.Data.Count > 0)
    {
        root.Add
        (
            new XElement("Data",
                from entry in
                    ex.Data.Cast()
                let key = entry.Key.ToString()
                let value = (entry.Value == null) ?
                    "null" : entry.Value.ToString()
                select new XElement(key, value))
        );
    }

    queryString = Convert.ToBase64String(encoding.GetBytes(root.ToString()));

    Response.Redirect("/admin/Error.aspx?ex=" + Server.UrlEncode(queryString));
}

Of course, if anything goes wrong encoding your error, it will generate a new error, which attempts to do the same thing, usually in a circle of recursive fail.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!