質問

We have an ASP.NET MVC 3 application that is configured to use ELMAH. We also have code in our Application_Error method like this. Both ELMAH and our custom code log to a database.

protected void Application_Error(object sender, EventArgs e)
{
    MvcApplication app = (MvcApplication)sender;
    HttpContext httpContext = app.Context;

    Exception ex = app.Server.GetLastError();
    HttpException httpException = ex as HttpException;              

    //log the error with our custom logging

    Server.ClearError();

    if (httpContext.IsCustomErrorEnabled) //only show custom error if enabled in config
    {
        httpContext.Response.Clear();
        httpContext.ClearError();

        //show our own custom error page here

    }
}

The problem (not really a problem, but whatever) we see is that both ELMAH and our custom code log the exception to the DB. I would expect calls to Server.ClearError() and httpContext.ClearError would handle the error and it would never get to ELMAH. But, does the fact that the error is being logged twice imply that ELMAH and application_error are running basically in parallel and they both receive the unhandled exception at the same time? If so is there anyway to tell ELMAH to ignore the error?

Our intent is to only have ELMAH handle an error if something goes REALLY wrong, like in the ASP.NET pipeline after elmah is registered, but before the MVC application would be running.

役に立ちましたか?

解決

The issue is that it is logging to ELMAH first. So yes you can tell it not to log to ELMAH using e.Dismiss(): The ErrorLog_Filtering function below is hit before Application_Error. So add this function and any needed logic you need to determine if you want it in ELMAH or not.

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    //get the exceptions like:
    Exception m = e.Exception;
    Exception ex = e.Exception.GetBaseException();

    //tell it not to log the error in ELMAH like (based on whatever criteria you have):
    e.Dismiss();

    //Application_Error will be hit next
}

protected void Application_Error(object sender, EventArgs e)
{
    //your logic
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top