Run log method whenever Exception ASP.NET MVC (Exception Handling) occurs

Asked

Viewed 459 times

3

I need to implement Error Log in my application ASP.NET MVC, My question is the following, is there any way I could trigger a method (This would be a method that would save the error log, send the email and related the treatment of Exception) whenever a Exception anywhere in my code not necessarily this being inside a block of Try catch. 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 Gravarlogerro() in only one part of the application, and not in all my Blocks of Try Catch, because if I implement one more parameter in this method I would have to turn all the Try Catch of the application to be able to inform the parameter.

I hope you understand me. Thank you very much.

  • 2

    From what I understand Cvoce has the option to configure this in Global.asax eou Web.config, and get the errors via Exception ex = Server.Getlasterror(); see this post in stackoverflow: http://stackoverflow.com/questions/343014/asp-net-custom-error-page-server-getlasterror-is-null

  • @Edenilson I also always had this doubt when creating my solutions for logs. And I use Web Forms. So if anyone can give an answer considering the Web Forms.

  • @Diegomoreno see this link, It teaches the same various application types will suit you: https://msdn.microsoft.com/en-us/library/bb397417.aspx

1 answer

3


People got it as follows: To capture errors in ASP.NET MVC in Global Asax you have to put the following method: The following link teaches how to use it in other application types: https://msdn.microsoft.com/en-us/library/bb397417.aspx

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

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

  • could also make a Global filter. -or create attributes if only in some controllers want to put this log system, something like

Browser other questions tagged

You are not signed in. Login or sign up in order to post.