ASP.NET UpdatePanel Timeout and 500 errors with custom errors
I have a .net application that uses customerrors web.config module to display meaningful messages for errors. It works without any issues for 500 errors/exceptions caused by non-ajax and ajax components (updatepanel). However, in a scenario where updatepanel's asynchronous request times out, there is no error raised at all. I was able to see the timeout in firebug and come up with a solution that would at least display the error message as an alert and then redirect the user to the 500 error page using javascript but it's not quite doing what the rest of the application does in case of an unhandled errors like these. I basically just want everything to go through "LogEvent" mechanism so based on the severity of the error, it does the necessary work.
Here are my questions:
- This 500 error page doesn't have anything in the Server.GetLastError() for these timeout scenarios. Is this an expected behaviour?
- Can it be changed so I do have access to these timeouts in Server.GetLastError() OR maybe just run this error through "LogEvent" mechanism?
- Is there a better/more graceful way to handle this issue?
Below is my code to give you an idea, not exactly what I have in my application but pretty close.
Web.Config
<customErrors mode="On" defaultRedirect="~/Errors/ErrorUnknown.aspx" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="~/Errors/Error500.aspx" />
</customErrors>
Error500.aspx.vb
Dim lastException As Exception = Server.GetLastError()
If lastException IsNot Nothing Then LogEvent(LogLevel.Error, "UnHandled Error From 500 Error Page.", lastException)
LblErrorMessage.Text = "Unknown error occured when processing your request."
Session.Abandon()
Page With UpdatePanel ASPX
<asp:ScriptManager ID="AppScriptManager" AsyncPostBackTimeout="3" runat="server" AllowCustomErrorsRedirect="True" OnAsyncPostBackError="AppAsyncPostBackError" AsyncPostBackErrorMessage="There was an error processing your request!" />
Javascript on the Page with UpdatePanel ASPX
<script type="text/javascript" language="javascript">
$(document).ready(function () {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
});
function endRequestHandler(sender, args) {
if (args.get_error() != undefined) {
var errorMessage = "There was an error processing your request!";
errorMessage += '\nError message: ' + args.get_error().message;
args.set_errorHandled(true);
alert(errorMessage);
window.location.replace('Errors/Error500.aspx');
}
}
</script>
Page With UpdatePanel ASPX.VB
Public Sub AppAsyncPostBackError(sender As Object, e As System.Web.UI.AsyncPostBackErrorEventArgs) Handles AppScriptManager.AsyncPostBackError
LogEvent(LogLevel.Fatal, "Asyncpostback error from update panel.", e.Exception)
End Sub
Function to log events using NLog
Private Sub LogEvent(level As LogLevel, logMessage As String, exception As Exception, ParamArray args() As Object)
Try
Dim appLogger As Logger = LogManager.GetCurrentClassLogger()
If exception IsNot Nothing Then
'# If it's an exception store it in a seperate file as fatal exception
开发者_开发技巧 appLogger.LogException(level, String.Format(logMessage, args), exception)
'# send an email
SendErrorEmail(String.Format(logMessage, args), exception)
'# Throw Exception as this was fatal
If level = LogLevel.Fatal Then Throw
Else
'# Log the message
appLogger.Log(level, logMessage, args)
End If
Catch ex As Exception
End Try
End Sub
Yes, the server error 500 is something to do with the web server.
Read this knowledge base from Microsoft: "How Web site administrators can troubleshoot an "HTTP 500 - Internal Server Error" error message on IIS 4.0 or on IIS 5.0"
And please don't mess up with HTTP error messages. Error 500 is usually known when error happens at web server, not the application. Therefore, giving error 500 for your redirects is not recommended, it will confuse web administrator and also developers.
This is the reason: "Description of Hypertext Transport Protocol error messages"
Your best bet to handle this issue is having logging mechanism such as the one in Log4Net (log for .NET), or use logging in Microsoft's Enterprise Library 5.0. Before that, you can use custom error/exception pages but don't handle error 500.
精彩评论