i have developed a web application by using C# rezor MVC4. it is working fine. some time client says some pages shows error while when i checked the page it shows properly. what and when exactly error come client is not able to tell me. i want to know how do get that error when it was generated. is there any way by which that error should be email to me when it genrates
Thanks!
Surendra
Alok SaxenaPosted Aug 19, 2014, 11:20 AM
Application_Error event, can use to catching error either in log file or plain text file or store in db or email too.
Step-1 global.asax
void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
if (error.Message != "Not Found")
{
// Send email here...
}
}
Step-2 web.config
set customErrors="Off
Step-3
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute()); // Must Remove this line,
}
...
}
Why Remove this line
By default (when a new project is generated), an MVC application has some logic in the Global.asax.cs file. This logic is used for mapping routes and registering filters. By default, it only registers one filter: a HandleErrorAttribute filter. When customErrors are on (or through remote requests when it is set to RemoteOnly), the HandleErrorAttribute tells MVC to look for an Error view and it never calls the Application_Error() method
To get the ApplicationError() method called for every unhandled exception, simple remove the line which registers the HandleErrorAttribute filter.
Regards
Alok Saxena