2014-07-25 34 views
1

在我的ASP.NET MVC应用程序中,我有一个非常具体的操作,我不希望Elmah处理它,因为它可以发出500个响应状态代码作为其正常函数的一部分。我可以在Elmah过滤器中获得所需的路线吗?

我不希望被这条路线中发生的错误所滥用,但我也不想在其他地方关闭Elmah logging 500错误。

有没有办法在web.config中声明执行此操作,还是必须编写我自己的Assertion? 或者,换一个说法,有没有办法在测试断言中获得绑定的路由? PS:对于那些想知道为什么我会允许我的行为生成500个,这是因为它是围绕客户定义的代码的包装,并且我希望我的客户立即知道他们的自定义代码是否有问题。

回答

1

它看起来像你正在从该特定操作引发未处理的异常。我想是这样的,因为如果你只返回一个500错误代码没有例外,ELMAH不会记录它:

public ActionResult ThrowError() 
{ 
    //your wrapper calling customer code   

    return new HttpStatusCodeResult(500, "Buggy code"); 
} 

所以我假设你的动作抛出未处理的异常(可能让你的客户可以看到异常的详细信息? )。 Let's说你的操作方法是在HomeController,看起来像这样:

public ActionResult ThrowError() 
{  
    //your wrapper calling customer code 
    // the customer code throws an exception: 
    throw new Exception("Buggy code"); 
} 

你去/Home/ThrowError每当你将得到一个未处理的异常导致500,由ELMAH记录。所以,你需要从ELMAH,它给你the following options

你可以添加对ELMAH以下筛选您global.asax,告诉ELMAH忽略在那个特定的URL例外过滤此:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e) 
{ 
    var httpContext = HttpContext.Current; 
    if(httpContext == null) return; 

    if (httpContext.Request.Url.AbsolutePath == "/home/throwerror") e.Dismiss(); 
} 

如果已经启用在ErrorFilter模块,你可以做同样在web.config:

<system.web> 
    <httpModules> 
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> 
    </httpModules> 
</system.web> 

<system.webServer> 
    <modules> 
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> 
    </modules> 
</system.webServer> 

<elmah> 
    <errorFilter> 
     <test> 
     <and> 
      <equal binding="Context.Request.ServerVariables['URL']" value="/home/throwerror" type="String" /> 
      <!--You could also use a regex for the url--> 
      <!--<regex binding="Context.Request.ServerVariables['URL']" 
        pattern="/home/throwerror\d*" />--> 
     </and> 
     </test> 
    </errorFilter> 
</elmah> 

我不很喜欢这种方法依赖于上市要筛选异常的所有网址(或进行确保所有这些URL都遵循一种可以在正则表达式上表达的模式)。如果您可以将从客户代码抛出的异常封装为特定异常,那么您可以过滤所有未处理的该类型异常,而不管它们从哪个URL中抛出。

所以,你可以创建一个新的异常类型,并用它在你的代码包装customer's代码:

public class CustomerException : Exception 
{ 
    public CustomerException(Exception e) 
     : base(e.Message, e) 
    { 
    } 
} 

public ActionResult ThrowError() 
{ 
    try 
    { 
     //your wrapper calling customer code 
     // the customer code throws an exception: 
     throw new Exception("Buggy code"); 
    } 
    catch (Exception e) 
    { 
     throw new CustomerException(e); 
    } 
} 

现在你可以从被记录在ELMAH无论是在global.asaxweb.config过滤那些例外:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e) 
{ 
    var httpContext = HttpContext.Current; 
    if(httpContext == null) return; 

    if (e.Exception is CustomerException) e.Dismiss(); 
} 

<errorFilter> 
    <test> 
    <jscript> 
     <expression> 
     <![CDATA[ 
     // @assembly mscorlib 
     // @assembly WebApplication3 
     // @import WebApplication3.Controllers 

     // In my dummy project, CustomerException is located in WebApplication3.Controllers 
     $.Exception instanceof CustomerException 
     ]]> 
     </expression> 
    </jscript> 
    </test> 
</errorFilter> 

现在用最后一种方法,你不依赖于动作网址。

希望这有助于!

相关问题