需要确定ELMAH是否记录未处理的异常或ErrorSignal.Raise()引发的异常

时间:2010-05-25 14:06:54

标签: c# .net asp.net exception-handling elmah

我在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事件吗?

1 个答案:

答案 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));