C# 需要确定ELMAH是记录未处理的异常还是由ErrorSignal.Raise()引发的异常

C# 需要确定ELMAH是记录未处理的异常还是由ErrorSignal.Raise()引发的异常,c#,.net,asp.net,exception-handling,elmah,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 exec

当发生未处理的异常时,我正在使用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

我遇到的问题是,对于未处理的异常和这些已处理、引发的异常,都会触发记录的事件。在记录的事件处理程序中,是否有方法确定异常是通过ErrorSignal类引发的,还是只是未处理的?还有其他Elmah事件可以利用吗?

厌倦了寻找“正确”的方法,因此我最终创建了自己的异常类型:

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