因此,我创建了一个自定义异常类(我们称之为CustomException
)以及Exception
该类中未找到的一些自定义属性。在 global.asax.cs 文件中有Application_Error
一个在异常发生时调用的方法。我Server.GetLastError()
用来抓取触发该Application_Error
方法的异常。问题是Server.GetLastError()
它只抓取一个Exception
对象,而不是CustomException
与它的自定义属性一起被抛出的对象。基本上被检索时CustomException
被剥离为一个对象,因此失去了与 关联的自定义属性。Exception
Server.GetLastError()
CustomException
有没有办法GetLastError()
实际检索CustomException
对象而不是精简Exception
版本?为了将错误存储在具有比通常由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
}