我在Global.asax文件中使用Elmah Logged事件,在发生未处理的异常时将用户转移到反馈表单。
有时我会记录其他处理的异常。例如:
ErrorSignal.FromCurrentContext().Raise(new System.ApplicationException("Program code not found: " + Student.MostRecentApplication.ProgramCode));
// more code that should execute after logging this exception
我遇到的问题是,对于未处理的和这些处理的,引发的异常,都会触发Logged事件。有没有办法在Logged事件处理程序中确定异常是通过ErrorSignal类引发还是简单地未处理?我可以利用其他Elmah事件吗?
答案 0 :(得分:2)
厌倦了尝试找到“正确”的方法来做到这一点,所以我最终创建了自己的异常类型:
public class HandledElmahException : Exception
{
public HandledElmahException() : base() { }
public HandledElmahException(string message) : base(message) { }
public HandledElmahException(string message, Exception innerException) : base(message, innerException) { }
}
然后,在ErrorLog.Logged事件处理程序中,我只是检查异常是否为HandledElmahException
类型。
void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
if (args.Entry.Error.Exception is HandledElmahException)
return;
// my code to transfer to custom error page to collect feedback...
}
因此,如果我不想将它们带到ErrorPage,当记录异常时,我会使用我的特殊HandledElmahException
类的实例,该类可以派生自。
ErrorSignal.FromCurrentContext().Raise(new HandledElmahException("Program code not found: " + Student.MostRecentApplication.ProgramCode));