I am making a module that logs the errors to a .log
file and save it to the database. I am only triggering the logging under this following instances:
- Ajax errors. More specifically, error 500 status code that has occurred on the controller.
- When the user tries to go to a non-existent page within the web application
The Problem:
My 2nd goal works very well but I am having trouble with my 1st goal. The Server.GetLastError()
returns null
when an Internal Server error has occurred. In addition to this, it doesn't redirect the custom pages, it redirects on the _ViewStart.cshtml
instead.
I have tried the solutions from this post but none seems to work. It still returns null
and not the exception message. This only happens on Internal Server error (500) I think. Below is my code, the web config, and a test endpoint to simulate an error.
Global.asax
void Application_Error(object sender, EventArgs e)
{
try
{
// Code that runs when an unhandled error occurs
HttpException lastErrorWrapper = Server.GetLastError() as HttpException;
Exception lastError = lastErrorWrapper;
if (lastErrorWrapper.InnerException != null)
lastError = lastErrorWrapper.InnerException;
string lastErrorTypeName = lastError.GetType().ToString();
string lastErrorMessage = lastError.Message;
string lastErrorStackTrace = lastError.StackTrace;
// Creation of the log and the log file containing the error
LogFileModel logFile = helpers.CreateLogFile(lastErrorTypeName, lastErrorMessage, lastErrorStackTrace);
helpers.InsertActivityLog_Error(2, "", "Web portal error.", logFile.file, logFile.fileSize, logFile.fileName);
}
catch (Exception) { }
}
Web.config
<system.web>
<customErrors defaultRedirect="~/Error/ErrorHasOccured" mode="RemoteOnly" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/Error/PageNotFound"/>
<error statusCode="500" redirect="~/Error/ErrorHasOccured"/>
</customErrors>
</system.web>
Test end-point on one of my Controllers
public ActionResult test() {
throw new Exception("This is a test error!");
return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
}
If I missed something on the post I linked above, please let me know. Any help would be appreciated. Thanks!