Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elmah not logging exceptions

In my MVC web project. I am trying to show custom error pages to my visitors without using "custromerrors" element in web.config.

I can catch exceptions like below

protected void Application_Error(object sender, EventArgs e)
{

    Exception exception = Server.GetLastError();

    bool success = RaiseErrorSignal(exception);

    Response.Clear();

    HttpException httpException = exception as HttpException;

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception, Let's handle it.
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                // Page not found.
                routeData.Values.Add("action", "Error404");
                break;
            case 500:
                // Server error.
                routeData.Values.Add("action", "Error500");
                break;

            // Here you can handle Views to other error codes.
            // I choose a General error template  
            default:
                routeData.Values.Add("action", "Index");
                break;
        }
    }

    // Pass exception details to the target error View.
    routeData.Values.Add("error", exception);

    // Clear the error on server.
    Server.ClearError();

    // Call target Controller and pass the routeData.
    IController errorController = new ProjectName.WebSite.Controllers.ErrorController();
    errorController.Execute(new RequestContext(
         new HttpContextWrapper(Context), routeData));


}

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;
}

But Elmah cant log errors also i am raising error signal.

like image 868
Yucel Avatar asked Jan 06 '11 14:01

Yucel


People also ask

How do you check ELMAH error?

Build the application, run it in the browser, and navigate to http://www.yoursite.com/elmah.axd. You are prompted to log in before you see the content. After a successful authentication, you see a web page to remotely view the entire log of recorded exceptions.

What is ELMAH logging?

ELMAH provides a simple, yet powerful mechanism for logging errors in an ASP.NET web application. Like Microsoft's health monitoring system, ELMAH can log errors to a database and can send the error details to a developer via email.

Does ELMAH work with .NET core?

ELMAH doesn't support ASP.NET Core.


1 Answers

I found the problem, I missed a web.config section. I added "ErrorLog" module to <system.webserver><modules>.

I also need to add it to <system.web><httpModules>.

After adding, Elmah start to log errors.

Also I don't need to call ErrorSignal.Raise() method, Elmah can detect errors without signalling.

like image 54
Yucel Avatar answered Sep 20 '22 07:09

Yucel