Wednesday, March 24, 2010

Fiddler output for ELMAH

Fiddler is a great web debugger for web developers of any platform. ELMAH is great for error logging in ASP.NET apps. Both are practically must-have tools for any ASP.NET developer. So how about combining them to debug ASP.NET errors more easily?

Here's a module that sends a SAZ file attached to all ELMAH mails. If you're not familiar with Fiddler, SAZ stands for Session Archive Zip, it's basically a ZIP file containing raw HTTP request/responses. After installing this module, a sample ELMAH mail might look like this:

elmah-mail

See the last attachment? It's our SAZ file, click on it to open it with Fiddler:

fiddler-password

The SAZ is password-protected since the HTTP form might have sensitive information. Enter the password and you can see the request:

fiddler-1

Now you can edit the request from Fiddler, change the host to your local instance of the website and then replay the request to reproduce the error:

fiddler-2

Configuration is very easy: just register the ElmahMailSAZModule after ELMAH's ErrorMailModule. You can optionally supply a configuration, e.g.:

<configuration>
    <configSections>
        <sectionGroup name="elmah">
            <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
            <section name="errorMailSAZ" requirePermission="false" type="ElmahFiddler.ElmahMailSAZModule, ElmahFiddler"/>
        </sectionGroup>
    </configSections>
    <elmah>
    <errorMailSAZ>
      <password>bla</password>
      <exclude>
        <url>default</url>
        <url>blabla</url>
      </exclude>
    </errorMailSAZ>
    <errorMail
      from="pepe@gmail.com"
      to="pepe@gmail.com"
      subject="ERROR From Elmah:"
      async="false"
      smtpPort="587"
      useSsl="true"
      smtpServer="smtp.gmail.com"
      userName="pepe@gmail.com"
      password="pepe" />
  </elmah>
...

This will apply the password "bla" to the SAZ files, and NOT create any SAZ for any requests that match (regex) "default" or "blabla". The latter is useful to prevent potentially huge SAZ files coming from requests with file uploads.

Caveats:

  • Requires async="false" on the mail module, since it needs access to the current HttpContext.
  • Does not include the HTTP response. This could be implemented using Response.Filter, but I'm not sure it's worth it.

I'm also playing with the idea of keeping a trace of all the requests in a user session, in order to reproduce more complex scenarios (SAZ files can accomodate multiple requests). This would place a considerable load on the server though, and the resulting SAZ file could get quite big.

Source code is here. It's a VS2010 / .NET 4.0 solution.

Kudos to Eric Lawrence for recently implementing SAZ support in FiddlerCore, without it this wouldn't be possible.

11 comments:

LogMyTime Zeiterfassung - Adrian Grigore said...

Thanks a lot! I had the same idea and was hoping someone would had implemented this already. I'm really glad I found this.

Adrian

Adrian Grigore said...

I just tried running the demo, but it does not seem to work for me - ElmahFiddler.SAZ.WriteSessionArchive always returns false because arrSessions is always null. What am I doing wrong?

Thanks,

Adrian

Mauricio Scheffer said...

@Adrian: are you using the trace module or the other one? are you using IIS7?

Adrian Grigore said...

Thanks for your help. I am using IIS7. I am not sure what you mean by the trace module though. I just took your demo project, extracted it to my hard drive, added an IIS application pointing to the web app project's folder and ran the project in a debugger. Is there anything I can look up in the configuration? BTW: you can also reach me via e-mail at adrian@lobstersoft.com if you prefer. Thanks, Adrian

Mauricio Scheffer said...

@Adrian: there are two modules. One of them creates a SAZ with just the request that caused the error. The other one (ElmahMailSAZTraceModule) includes the whole trace of requests by that user up to the request that caused the error. Currently in SVN, the latter is the one configured in the web.config. Also, you need to configure your email settings in web.config and make sure this is running on a .NET 4.0 application pool.

Rajani said...

Hello There..

pls can u show a way where i can merge attributes across cores into single DOC not just merging cores; it will not merge attributes in same single doc..

I split my data in different cores wid different schemas in hierarchy..
Now i want to perform search across all cores that i am doing using Shards concepts..
But i have a problem that is regarding Logical operator..
I cannot perform AND operation across cores..
The result will fail..(as data in cores different)
Is there any way of dynamic merging of attributes..So that i can fix this logical operator problem..



Regards,

Rajani Maski

Mauricio Scheffer said...

@Rajani: I assume you're talking about SolrNet (this post is about Fiddler and ELMAH). Please use the mailing list for questions about SolrNet: http://groups.google.com/group/solrnet

Adrian Grigore said...

@Mauricio: Thanks for your help with setting up ElmahFiddler, the problem was indeed that my app pool was running with .NET 2.0.

Today my production server halted my website's application pool. This was caused by the ElmahFiddler plugin. A user had a HTML entity in a cookie, which caused a System.Web.HttpRequestValidationException. Since there is no HTTPContext when this exception is raised, ElmahFiddler crashes when trying to create the e-mail attachment. This in turn crashes W3P, and after 5 W3P Crashes the IIS Rapid Fail Protection kicked in.

It's quite easy to solve the problem though: Just check inSessionStart, MailModuleMailed and MailModuleMailing if there actually is a HTTPContext. I just wanted to let you know of this so so you can update the source code in your blog accordingly.

Best Regards,

Adrian Grigore

Mauricio Scheffer said...

@Adrian: weird that it doesn't have a context! Anyway, I applied your suggestions, thanks!

Robert said...

Seem to be having an issue with implementing this. I think it's because the application is actually a web site rather than a web application though.

Mauricio Scheffer said...

@Robert: I have not tested this on Web Site Projects. To be perfectly honest, I think Web Site projects are a terrible idea. Just stick to Web Application projects.