elmah_ELMAH:ASP.NET的错误记录模块和处理程序(也包括MVC!)

elmah_ELMAH:ASP.NET的错误记录模块和处理程序(也包括MVC!)_第1张图片

elmah

Joe Lowrance said, er tweeted, it best when he said,

乔·劳伦斯(Joe Lowrance)说,呃,最好说,

"the amount of attention ELMAH hasn't got is shocking."

“ ELMAH尚未引起的关注令人震惊。”

ELMAH is one of those largely unknown and deeply awesome .NET Open Source projects that should be part of ASP.NET proper.

ELMAH是应为ASP.NET一部分的,广为人知且功能强大的.NET开源项目之一。

什么是ELMAH? (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?

我想说ELMAH是您的ASP.NET错误的Tivo 。 您可以获取完整的调用堆栈并获得死亡的黄屏,并分析实际发生​​的情况。 这是异常驱动开发。 它到底在做什么?

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:

将ELMAH放入正在运行的Web应用程序中并进行适当配置后,您将获得以下便利,而无需更改代码的一行:

  • 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 customErrors mode turned off.

    在许多情况下,即使关闭customErrors模式,您也可以查看ASP.NET为给定异常生成的原始黄色死亡屏幕

  • An e-mail notification of each error at the time it occurs.

    每种错误发生时的电子邮件通知。

  • An RSS feed of the last 15 errors from the log.

    日志中最后15个错误的RSS提要。

  • A number of backing storage implementations for the log, including in-memory, Microsoft SQL Server and several contributed by the community.

    日志的许多后备存储实现,包括内存, Microsoft SQL Server社区提供的一些实现

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.

创建者与Atif阿齐兹( @raboof在Twitter) 斯科特·米切尔 , ELMAH手段是“错误日志记录模块和处理”,并从已经摇摆了我的世界,那么, 2004年的九月。

(Small Correction, Scott Mitchell helped writing the original MSDN article. ELMAH is 100% conceived of by Atif.)

(小改正,斯科特·米切尔(Scott Mitchell)协助撰写了MSDN的原始文章。艾蒂夫(Atif)认为ELMAH是100%构思的。

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(错误记录模块和处理程序)是应用程序范围的错误记录工具,可完全插入。 可以将其动态添加到计算机上正在运行的ASP.NET Web应用程序甚至所有ASP.NET Web应用程序中,而无需重新编译或重新部署。

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.

ELMAH之所以出色,有很多原因。 首先,因为它可以正常工作。 其次,因为这是有效使用HttpModules和HttpHandlers协同工作的绝佳示例。 第三,因为经过充分设计,您几乎可以从中获得任何想要在生产现场使用的东西。

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. ;)

我坐在咖啡馆里,将ELMAH添加到基于ASP.NET MVC的NerdDinner.com源代码中。 实际上,该站点是免费的,因为如果我不做任何棘手的工作,则无需重新编译源代码。 ;)

在ASP.NET(MVC或其他)站点上实现ELMAH (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.

我下载了ELMAH,然后将其DLL放在我的/ bin文件夹中(或您想放置库的任何地方)。 如果您不想,ELMAH不需要直接被您的项目引用。 最终,它只需要放在您的bin文件夹中就可以找到它。 您可以将它放在/ lib中,并根据需要创建一个生成事件,只要它以/ 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.

然后,我打开记事本的两个实例,然后将几个HttpHandlers和HttpModules复制到我现有的web.config中。 乏味,但直截了当。

Where you put these depends on if you're using IIS6 or IIS7, but the general idea is the handlers looks like this:

放置这些内容的位置取决于您使用的是IIS6还是IIS7,但是通常的想法是处理程序如下所示:


   
    
....
....

and the modules looks like this:

模块看起来像这样:


   
    
...
...

You can pare it down a bit if you don't need ErrorMail, for example. Then, I hit my site at http://localhost:xxxx/elmah.axd and I get this Error log, similar to what you'd see via Trace.axd.

例如,如果不需要ErrorMail,可以将其缩减一些。 然后,我在http:// localhost:xxxx / elmah.axd上访问了我的网站,并得到了此错误日志,类似于通过Trace.axd看到的内容。

Ok, no errors. Let's make some. I'll visit some messed up URLs and intentionally cause trouble...here's what ELMAH says now:

好的,没有错误。 让我们做些。 我将访问一些混乱的URL并有意引起麻烦...这是ELMAH现在所说的:

And I can drill down and see the Yellow Screen of Death (YSOD) as it "would have been."

而且我可以深入了解“死亡黄屏”(YSOD),因为它“本来应该是”。

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.

现在,这假设我的ASP.NET MVC站点没有任何错误处理可言。 ELMAH正在监视未处理的异常并将其记录下来,并将其保存在内存中。 我还可以将日志设置到SQL Server或VistaDB或SQLLite,这样它们就可以在应用程序回收之后使用。

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.

您当然可以确保对ELMAH页面的访问。 您还可以将错误放入您喜欢的RSS阅读器中,这是一项杀手级功能,恕我直言。

使ELMAH与ASP.NET MVC的“ HandleError”属性配合使用 (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.

在ASP.NET MVC中,有一个名为[HandleError]的属性,它将捕获控制器内部发生问题的所有内容并显示〜\ Shared \ Error.aspx视图。 但是,由于它“处理”了错误,因此对ELMAH隐藏了该错误。 请记住,[HandleError]是声明性的try / catch。 就我而言,我想让ELMAH处理。

There's two ways around this.

有两种解决方法。

One, I made a method in my Controller like this:

一个,我在控制器中创建了一个像这样的方法:

public ActionResult Trouble()
{
return View("Error");
}

In my web.config, I have a customErrors section like this:

在我的web.config中,我有一个customErrors部分,如下所示:


   
    
   

This sends folks to /Dinners/Trouble which shows the Error page, after ELMAH takes care of it.

在ELMAH处理之后,将人员发送到/ Dinners / Trouble,该页面显示“错误”页面。

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.

其次,作为一种高级技术,我可以编写自己的派生的HandleErrorWithELMAHAttribute,使用ELMAH的API记录错误,然后将异常传递给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 the StackOverflow question.

看起来Dan Swatik的博客已死。 :(更新:今天早上Dan Swatik恰巧在做类似的事情错误 Dan的Blog上的解决方案很好,是它是在Atif Aziz自己的帮助下编写的(记住,Atif是ELMAH的主要作者! )这是StackOverflow问题。

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:

我的天真解决方案在下面,但是Atif的解决方案更好,因为它向ELMAH的管道发出错误信号,而不是像我的日志一样直接记录日志。 这是更正确的版本:

namespace MvcDemo
{
using System;
using System.Web;
using System.Web.Mvc;
using Elmah;

public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);

var e = context.Exception;
if (!context.ExceptionHandled // if unhandled, will be logged anyhow
|| RaiseErrorSignal(e) // prefer signaling, if possible
|| IsFiltered(context)) // filtered?
return;

LogException(e);
}

private static bool RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
if (context == null)
return false;
var signal = ErrorSignal.FromContext(context);
if (signal == null)
return false;
signal.Raise(e, context);
return true;
}

private static bool IsFiltered(ExceptionContext context)
{
var config = context.HttpContext.GetSection("elmah/errorFilter")
as ErrorFilterConfiguration;

if (config == null)
return false;

var testContext = new ErrorFilterModule.AssertionHelperContext(
context.Exception, HttpContext.Current);

return config.Assertion.Test(testContext);
}

private static void LogException(Exception e)
{
var context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(e, context));
}
}
}

Below is my naive version.

以下是我的幼稚版本。

I made this HandleErrorWithELMAHAttribute:

我做了这个HandleErrorWithELMAHAttribute:

public class HandleErrorWithELMAHAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
try
{
var context = HttpContext.Current;
Error error = new Error(filterContext.Exception, context);
ErrorLog log = ErrorLog.GetDefault(context);
string id = log.Log(error);
}
catch (Exception localException)
{
//
// IMPORTANT! We swallow any exception raised during the
// logging and send them out to the trace . The idea
// here is that logging of exceptions by itself should not
// be critical to the overall operation of the application.
// The bad thing is that we catch ANY kind of exception,
// even system ones and potentially let them slip by.
//

Trace.WriteLine(localException);
}

base.OnException(filterContext);
}
}

Then I put the attribute on my ASP.NET MVC Controller like this:

然后,将属性像这样放在ASP.NET MVC控制器上:

[HandleErrorWithELMAHAttribute]
public class DinnersController : Controller {
...

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.

无论哪种方法,我实际上都更喜欢第一种,尽管它要求控制器具有一个本质上为空的方法来将人员发送到共享错误页面。 我认为/ Confused和/ Trouble方法在那里,因为我认为它使网站更具个性。

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!)

无论如何,ELMAH摇摇欲坠,您今天应该使用它。 菲尔·哈克(Phil Haack)也爱ELMAH ! Jeff和StackOverflow的人员也使用ELMAH ,尽管这是“自定义分叉”。 (发布您的代码,Jeff! )

Time for me to to donate some money to the ELMAH Open Source efforts.

是时候让我为ELMAH开源活动捐款。

Related

有关

  • Elmah Project Site

    Elmah项目现场

  • Good Exception Management Rules of Thumb

    良好异常管理经验法则

翻译自: https://www.hanselman.com/blog/elmah-error-logging-modules-and-handlers-for-aspnet-and-mvc-too

elmah

你可能感兴趣的:(java,python,linux,spring,mysql)