【发布时间】:2014-03-18 17:40:48
【问题描述】:
我正在尝试使用在 try...catch 块中处理的 Elmah 异常记录。
我在Global.axax 上添加了一个全局句柄错误过滤器:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
}
这是我的ElmahHandledErrorLoggerFilter:
public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
}
}
它只会记录try{ ... }catch{ throw new Exception(); } 中的异常。但这不是问题,问题是我有一个带有 try-catch 的方法,该方法已从另一个 try-catch 中的代码调用。在这种情况下,虽然我将throw new Exception() 放在内部方法的 catch 中,但它不会记录异常,它会返回到第一个方法中的 catch 而不记录异常。例如:
public void MainMethod()
{
try
{
SecondMethod();
}
catch
{
....second method jump here.....
}
}
public void SecondMethod()
{
try
{
int a =0;
int b =;
int result = b/a;
}
catch
{
throw new Exception();
}
}
SecondMethod 抛出的异常没有被 Elmah 记录。它回到主方法catch。如果 main 方法 catch 也有 throw new Exception() 代码,那么它会记录异常。然而,它将被记录,堆栈跟踪指向MainMethod,而不是SecondMethod。
我想要的是每次它到达一个捕获而不重新抛出一个新的异常时,Elmah 会记录这个异常。这是因为我的应用程序中有许多 try-catch 块,我想记录这些异常,而不需要手动记录每个 try-catch。但是,如果您告诉我如何记录来自 SecondMethod 的异常,那就没问题了。
【问题讨论】:
标签: c# exception exception-handling try-catch elmah