parush / elmah

Automatically exported from code.google.com/p/elmah
0 stars 0 forks source link

Attach viewstate to email...Also, add other FORM variables to email. #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I found that sometimes it was handy to have the contents of viewstate when 
the error happened.  So, I modified the ErrorMailModule to attach the 
viewstate to the email as its own file (much like you attach the original 
error).

In the PreSendMail() method, add this:

            //WB attach viewstate to email...
            if (error.Form!=null )
                if (error.Form != null && error.Form["__VIEWSTATE"] != 
null && error.Form["__VIEWSTATE"].Length != 0)
            {
                //
                // Create a temporary file to hold the attachment. Note 
that 
                // the temporary file is created in the location returned 
by
                // System.Web.HttpRuntime.CodegenDir. It is assumed that
                // this code will have sufficient rights to create the
                // temporary file in that area.
                //

                string fileName = "ViewState-" + Guid.NewGuid().ToString() 
+ ".htm";
                string path = Path.Combine(HttpRuntime.CodegenDir, 
fileName);
                try
                {
                    using (StreamWriter attachementWriter = File.CreateText
(path))
                    {
                        attachementWriter.Write(error.Form["__VIEWSTATE"]);
                    }

                    mail.Attachments.Add(new Attachment(path));
                }
                catch (IOException)
                {
                    //
                    // Ignore I/O errors as non-critical. It's not the
                    // end of the world if the attachment could not be
                    // created (though it would be nice). It is more
                    // important to get to deliver the error message!
                    //
                }
            }

Also, in ErrorDetailPage class, RenderError() method, add:
            //WB, lets write out form!
            if (error.Form != null && error.Form.Count > 0)
            {
                NameValueCollection tempForm = new NameValueCollection
(error.Form);
                tempForm.Remove("__EVENTTARGET");
                tempForm.Remove("__EVENTARGUMENT");
                tempForm.Remove("__VIEWSTATE");
                tempForm.Remove("__EVENTVALIDATION");
                RenderCollection(writer, tempForm,
                    "FormData", "Form");

                //WB Render the viewstate also
                if (error.Form["__VIEWSTATE"] != null && error.Form
["__VIEWSTATE"].Length != 0)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Br);
                    writer.RenderBeginTag(HtmlTextWriterTag.B);
                    writer.Write("ViewState:");
                    writer.RenderEndTag(); // </b>
                    writer.AddAttribute
(HtmlTextWriterAttribute.Id, "ErrorDetail");
                    writer.RenderBeginTag(HtmlTextWriterTag.Pre);
                    writer.Flush();
                    Server.HtmlEncode(error.Form["__VIEWSTATE"], 
writer.InnerWriter);
                    writer.RenderEndTag(); // </pre>
                    writer.WriteLine();
                }
            }

finally, in ErrorMailHtmlFormatter the method RenderCollections() add:
            //WB, lets write out form!

            if (Error.Form!=null && Error.Form.Count>0)
            {
                NameValueCollection tempForm = new NameValueCollection
(Error.Form);
                tempForm.Remove("__EVENTTARGET");
                tempForm.Remove("__EVENTARGUMENT");
                tempForm.Remove("__VIEWSTATE");
                tempForm.Remove("__EVENTVALIDATION");
                RenderCollection(tempForm, "FormData");
            }

Original issue reported on code.google.com by wayne.br...@gmail.com on 26 Jun 2007 at 4:03

GoogleCodeExporter commented 9 years ago
Changed the type of this issue from defect to enhancement.

Original comment by azizatif on 26 Jun 2007 at 7:39

GoogleCodeExporter commented 9 years ago
I like the idea to have the Request like it is on the server, therefore I 
simple 
take the stream and send it as an attachment: 
email.Attachments.Add(New Net.Mail.Attachment(Request.InputStream, 
"request.txt"))

Original comment by Daniel.C...@gmail.com on 26 Jun 2007 at 7:51

GoogleCodeExporter commented 9 years ago
ErrorMailModule has been enhanced with three new events, namely Mailing, Mailed 
and 
DisposingMail that should allow an application to add additional information to 
the 
error report mail based before it is dispatched. The integration is not as 
tight as 
in the opened issue but since each application will have varying degrees of 
requirements for this feature, the event-based approach seemed the simplest way 
to 
get the biggest bang for the buck. The events can be used in Global.asax to 
create 
additional attachments. For example, view state can be tagged on the mail like 
this 
(assuming ASP.NET 2.0):

void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
    string viewState = e.Error.Form["__VIEWSTATE"];
    if (string.IsNullOrEmpty(viewState))
        return;
    e.Mail.Attachments.Add(Attachment.CreateAttachmentFromString
(viewState, "ViewState.txt", Encoding.UTF8, "text/plain"));
}

Original comment by azizatif on 29 Jun 2007 at 1:50

GoogleCodeExporter commented 9 years ago
What about getting this data to the other error providers, like database?

Maybe there would be a more generic 'Error_Outputting' event that would just 
let you 
add text or streams.  You could have properties like 'filestreams' and 
'othertext'.  
The othertext would be extra text in the body of the email or body of the 
database 
entry.  The filestreams would be turned into attachments in the email and 
handled 
appropriately in the other output formats....

Original comment by wayne.br...@gmail.com on 31 Jan 2008 at 4:33

GoogleCodeExporter commented 9 years ago
These are certainly suggestions worth considering in a future release, but why 
not 
post these as a new issue instead of adding to a closed one here?

Original comment by azizatif on 31 Jan 2008 at 5:24

GoogleCodeExporter commented 9 years ago
I posted it here because it is related to this and this solution.   Be able to 
attach things to be put logged to whatever provider is provided.  Would you 
like me 
to make a new one?

Original comment by wayne.br...@gmail.com on 31 Jan 2008 at 10:34

GoogleCodeExporter commented 9 years ago
> Would you like me to make a new one?

Yes, please, since this issue is specific in its own way (even if similar to 
the one 
you're proposed), addressed and therefore closed. Since it's closed, it doesn't 
appear in the pending issues list and therefore your requests risks getting 
forgotten.

> posted it here because it is related to this and this solution

You can always cross-reference this issue in the new one to mention how they 
may be 
similar.

Thanks.

Original comment by azizatif on 1 Feb 2008 at 8:16