そのため、カスタム例外クラス ( と呼びましょう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
}