Execute log method every time ASP.NET MVC (Exception Handling)

3

I need to implement Error Logging in my ASP.NET MVC application, , My question is as follows, is there any way I can trigger a method (would this be a method that would write the error log, e-mail, and the like exception handling) whenever an Exception occurs anywhere in my code, it is not necessarily within a try catch block. Example:

//Este método fica na "Escuta" a espera de que ocorra uma exception em qualquer parte do código
public void Exception_Listener(Exception ex){
//Grava o log de erro
GravarLogErro(ex);
}

The idea of this is that I use the call of the method BurnLogErro () in only one part of the application, and not in all my Blocks of Try Catch, because if I implement an extra parameter in this method I would have to turn all the Try Catch of the application to inform the parameter.

I hope you understand me. Thank you very much.

    
asked by anonymous 15.01.2017 / 19:14

1 answer

3

Personally I got it as follows: To catch errors in ASP.NET MVC in Global Asax has to by the following method: The following link teaches you to use the same in other types of applications: link

private void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError()
        //Seu método de gravar log
    }

Every time an exception is thrown it will automatically fall into this method. In my case it worked perfectly.

    
15.01.2017 / 21:48