C
C.
Hi,
I have an app that uses a Linq-to-Sql provider. If the database were
to go offline, I would like the user to be re-directed to a friendly
error page that reads from Session["ErrorMessage"], and I would like
to log the SqlException's details in the health monitoring system I
have set up so the admin is notified with an email.
My first thought was to handle the Application's Error event in the
Global.asax like so:
void Application_Error(object sender, EventArgs e)
{
Exception exc = Context.Server.GetLastError();
try
{
throw exc;
}
catch (System.Data.SqlClient.SqlException)
{
System.Web.Management.WebBaseEvent.Raise(new
ND.CustomEvents.DataStoreUnavailableError("Could not connect to the
data store", null, exc));
HttpContext.Current.Session["ErrorMessage"] = @"We are currently
experiencing difficulties with our database. Our administrator has
been notified, please check back soon.";
}
Context.Server.ClearError();
}
However, the exception that gets passed by GetLastError() is of type
"System.Web.HttpUnhandledException", which contains an inner exception
of "TargetInvocationException", which finally contains the
SqlException I want to handle.
Is there a better way of doing this? I could wrap each and every
method in my data access objects in a try/catch, but I'd like to
centralize my exception handling if at all possible.
Regards,
Chris
I have an app that uses a Linq-to-Sql provider. If the database were
to go offline, I would like the user to be re-directed to a friendly
error page that reads from Session["ErrorMessage"], and I would like
to log the SqlException's details in the health monitoring system I
have set up so the admin is notified with an email.
My first thought was to handle the Application's Error event in the
Global.asax like so:
void Application_Error(object sender, EventArgs e)
{
Exception exc = Context.Server.GetLastError();
try
{
throw exc;
}
catch (System.Data.SqlClient.SqlException)
{
System.Web.Management.WebBaseEvent.Raise(new
ND.CustomEvents.DataStoreUnavailableError("Could not connect to the
data store", null, exc));
HttpContext.Current.Session["ErrorMessage"] = @"We are currently
experiencing difficulties with our database. Our administrator has
been notified, please check back soon.";
}
Context.Server.ClearError();
}
However, the exception that gets passed by GetLastError() is of type
"System.Web.HttpUnhandledException", which contains an inner exception
of "TargetInvocationException", which finally contains the
SqlException I want to handle.
Is there a better way of doing this? I could wrap each and every
method in my data access objects in a try/catch, but I'd like to
centralize my exception handling if at all possible.
Regards,
Chris