ELMAH: Error Logging Modules and Handlers for ASP.NET (and MVC too!)
这玩意MS的官网也在推荐 顾谷歌了一把
原文:http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx
Joe Lowrance said, er tweeted, it best when he said,
"the amount of attention ELMAH hasn't got is shocking."
ELMAH is one of those largely unknown and deeply awesome .NET Open Source projects that should be part of ASP.NET proper.
What is ELMAH?
I like to say that ELMAH is Tivo for your ASP.NET Errors. You can get your Yellow Screens of Death, with full call-stack back and analyze what's really happening. It's Exception Driven Development. What's it really do?
Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilites without changing a single line of your code:
- Logging of nearly all unhandled exceptions.
- A web page to remotely view the entire log of recoded exceptions.
- A web page to remotely view the full details of any one logged exception.
- In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrorsmode turned off.
- An e-mail notification of each error at the time it occurs.
- An RSS feed of the last 15 errors from the log.
- A number of backing storage implementations for the log, including in-memory, Microsoft SQL Server and several contributed by the community.
Created by Atif Aziz (@raboof on Twitter) and Scott Mitchell, ELMAH means is "Error Logging Modules and Handlers" and has been rocking my world since, well, September of 2004.
(Small Correction, Scott Mitchell helped writing the original MSDN article. ELMAH is 100% conceived of by Atif.)
From the project site:
ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
ELMAH is wonderful for many reasons. First, because it just works. Second, because it's a fantastic example of effective use of HttpModules and HttpHandlers working together. Third, because it's been design with enough thought that nearly anything you'd want from it for use in a production site is there.
I'm sitting here in a cafe and I'm going to add ELMAH to the ASP.NET MVC-based NerdDinner.com Source Code. Actually, the site, as I don't need to recompile the source if I'm not doing anything tricky. ;)
Implementing ELMAH on your ASP.NET (MVC or otherwise) Site
I download ELMAH, and I put its DLL in my /bin folder (or wherever you like to put libraries). ELMAH doesn't need to be referenced by your project directly if you don't want to. Ultimately it just needs to be in your bin folder so it can be found. You can put it in /lib and make a build event if you like, as long as it ends up in /bin.
Then I open up two instances of notepad and copy over a few HttpHandlers and HttpModules into my existing web.config. Tedious, but straightforward.
Where you put these depends on if you're using IIS6 or IIS7, but the general idea is the handlers looks like this:
<handlers> .... <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" precondition="integratedMode" type="Elmah.ErrorLogPageFactory, Elmah"> .... </add></handlers>
<modules runallmanagedmodulesforallrequests="true"> ... <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"> ... </add></add></add></modules>
Ok, no errors. Let's make some. I'll visit some messed up URLs and intentionally cause trouble...here's what ELMAH says now:
And I can drill down and see the Yellow Screen of Death (YSOD) as it "would have been."
Now, this assumes my ASP.NET MVC site has no error handling to speak of. ELMAH is watching for unhandled exceptions and recording them, holding them in memory. I can also setup logs to SQL Server or VistaDB or SQLLite so they'll live beyond application recycles.
You can certainly secure access to the ELMAH pages. You can also pull the errors into your favorite RSS Reader, which is a killer feature, IMHO.
Making ELMAH work with ASP.NET MVC's "HandleError" Attribute
In ASP.NET MVC there's an attribute called [HandleError] that will grab anything that goes wrong inside your controllers and show the ~\Shared\Error.aspx View. However, because it "handles" the error, it hides it from ELMAH. Remember that [HandleError] is a declarative try/catch. In my case, I want to have ELMAH handle it.
There's two ways around this.
One, I made a method in my Controller like this:
public ActionResult Trouble() { return View("Error"); }
<customerrors mode="On" defaultredirect="/Dinners/Trouble"> <error statuscode="404" redirect="/Dinners/Confused"> </error></customerrors>
This sends folks to /Dinners/Trouble which shows the Error page, after ELMAH takes care of it.
Second, as an advanced technique, I could write my own derived HandleErrorWithELMAHAttribute that logs the error using ELMAH's API, then passes the exception up to the default handler with ASP.NET MVC.
Looks like Dan Swatik's blog is dead. :( UPDATE: Looks like Dan Swatik was coincidentally doing a similar thing error this morning! What's nice about the solution on Dan's Blog, is that it was written with the help of Atif Aziz himself! (Remember, Atif's The primary author of ELMAH!) Here's theStackOverflow question.
My naive solution is below, but Atif's is better as it signals the error to ELMAH's pipeline rather that logging directly as mine does. Here's the more correct version:
Below is my naive version.
I made this HandleErrorWithELMAHAttribute:
Then I put the attribute on my ASP.NET MVC Controller like this:
Either way works, I actually kind of prefer the first one, although it requires a controller to have an essentially empty method to send folks to the shared error page. I keep the /Confused and /Trouble methods in there as I think it makes the site more personal.
Anyway, ELMAH rocks and you should use it today. Phil Haack loves ELMAH too! Jeff and the guys at StackOverflow use ELMAH also, albeit a "custom fork." (Release your code, Jeff!)
Time for me to to donate some money to the ELMAH Open Source efforts.
Related
备注:
http://code.google.com/p/elmah/
http://www.cnblogs.com/lerit/archive/2011/04/19/2020565.html
http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx