它看起来像你正在从该特定操作引发未处理的异常。我想是这样的,因为如果你只返回一个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.asax
或web.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>
现在用最后一种方法,你不依赖于动作网址。
希望这有助于!