Elmah在第三个文件夹之后没有记录404错误,而根本没有403个错误

时间:2016-05-29 02:38:39

标签: asp.net-mvc error-handling asp.net-mvc-5 elmah

继续我的previous question,我使用以下方法处理我的mvc5应用中的自定义错误:

def newUsername(db):
    while True:
        username = input('Set an username:...')
        if not username:
            pass
        elif username in db:
            print("This user already exists!")
        else:
            return username

这使我能够在第3个文件夹之后显示自定义404错误页面(例如site.com/a/b/c/nothing-here)。然而,Elmah没有记录这些错误。 403错误也是如此。所以我的问题是:

  1. 有没有办法让Elmah处理所有服务器错误,无论如何 被mvc app本身抛出?
  2. 如果没有,由于 <system.webServer> <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="404"/> <error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL" /> <remove statusCode="403"/> <error statusCode="403" path="/Errors/NotFound" responseMode="ExecuteURL" /> <remove statusCode="500"/> <error statusCode="500" path="/Errors/ServerError" responseMode="ExecuteURL" /> </httpErrors> ... </system.webServer> 成功执行,我可以从该操作添加日志。有没有办法判断Elmah是否已经处理过这个错误(或者它是否被mvc app抛出)?

1 个答案:

答案 0 :(得分:0)

这里的问题是,ELMAH只记录未捕获的异常。在您的代码中,MVC实际上是使用自定义错误配置来处理错误。

如果您仍想记录这些异常,可以在MVC中实现IExceptionFilter并手动登录到ELMAH:

public class ElmahExceptionLogger : IExceptionFilter
{
    public void OnException (ExceptionContext context)
    {
        if (context.ExceptionHandled)
        {
            ErrorSignal.FromCurrentContext().Raise(context.Exception);
        }
    }
 }