So I've created a custom exception class (let's call it CustomException
) along with some custom properties not found in the Exception
class. In the global.asax.cs file there is the Application_Error
method that is called whenever an exception happens. I'm using Server.GetLastError()
to grab the exception that triggered the Application_Error
method. The problem is that Server.GetLastError()
only grabs an Exception
object and not the CustomException
object that is being thrown along with its custom properties. Basically the CustomException
is stripped down to an Exception
object when retrieved by Server.GetLastError()
, thus losing the custom properties associated with CustomException
.
Is there a way for GetLastError()
to actually retrieve the CustomException
object and not the stripped down Exception
version? This is needed in order to store the errors in a database table with more information than would normally be supplied by an Exception
.
Application_Error
:
protected void Application_Error(object sender, EventArgs e)
{
// This var is Exception, would like it to be CustomException
var ex = Server.GetLastError();
// Logging unhandled exceptions into the database
SystemErrorController.Insert(ex);
string message = ex.ToFormattedString(Request.Url.PathAndQuery);
TraceUtil.WriteError(message);
}
CustomException
:
public abstract class CustomException : System.Exception
{
#region Lifecycle
public CustomException ()
: base("This is a custom Exception.")
{
}
public CustomException (string message)
: base(message)
{
}
public CustomException (string message, Exception ex)
: base(message, ex)
{
}
#endregion
#region Properties
// Would like to use these properties in the Insert method
public string ExceptionCode { get; set; }
public string SourceType { get; set; }
public string SourceDetail { get; set; }
public string SystemErrorId { get; set; }
#endregion
}