使用Elmah在try..catch中处理日志异常 [英] Log exceptions handled in try..catch with Elmah

查看:214
本文介绍了使用Elmah在try..catch中处理日志异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用Elmah异常记录在 try ... catch 块中处理。在 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。在这种情况下,虽然我把在内部方法的catch中抛出新的Exception(),但它不会记录异常,它可以返回到第一个方法的catch没有记录异常。例如:

  public void MainMethod()
{
try
{
SecondMethod();
}
catch
{
....第二种方法跳过这里.....
}
}

public void SecondMethod()
{
try
{
int a = 0;
int b =;
int result = b / a;
}
catch
{
throw new Exception();
}
}

SecondMethod抛出的异常未被Elmah记录。它回到主要方法catch。如果main方法catch也有一个抛出新的Exception()代码,那么它会记录异常。但是,它将被记录,堆栈跟踪指向 MainMethod 而不是 SecondMethod



我想要什么,每次在不重新启动新异常的情况下,Elmah都会记录异常。这是因为我的应用程序中有很多try-catch块,我想记录这些异常,而不需要手动登录每个try-catch。但是,如果您告诉我如何从 SecondMethod 中记录异常。

解决方案

您是否使用ASP MVC?



过滤器只会执行从控制器方法抛出的未处理的异常。 ( context.ExceptionHandled 属性告诉您是否已被另一个过滤器处理,而不是在try-catch块中)。所以如果你在你的方法中的try-catch块中吞下异常,那么它们将不会被错误过滤器处理。



你需要决定什么时候手动处理您的代码中使用try-catch块的异常(在这种情况下,借助于基本控制器类或辅助类来手动记录异常),或者让过滤器处理异常。 (你可能会想要两者的混合,取决于每个特定的用例)



当你决定推翻异常时,看一下这个SO问题。基本上你可以重新抛出保存堆栈跟踪的异常:

  try 
{
// code
}
catch(Exception ex)
{
//...some错误处理代码在这里...
//否则为什么尝试catch?
throw;
}

您可以在样本中执行此操作 MainMethod 并且记录的异常将保留堆栈跟踪。


I'm trying to log with Elmah exceptions handled in try...catch blocks.

I have added a global handle error filter on Global.axax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new ElmahHandledErrorLoggerFilter());
    filters.Add(new HandleErrorAttribute());
}

This is my ElmahHandledErrorLoggerFilter:

public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
    public void OnException(ExceptionContext context)
    {
        if (context.ExceptionHandled)
            ErrorSignal.FromCurrentContext().Raise(context.Exception);
    }
}

It will only log the Exception as in try{ ... }catch{ throw new Exception(); }. But that's not the problem, the problem is that I have a method with a try-catch called from the code already inside another try-catch. In this case although I put throw new Exception() inside the catch of the inner-method it doesn't log the exception, it goes back to the catch in the first method without logging the Exception. For example:

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

The exception thrown by SecondMethod is not being logged by Elmah. It goes back to the main method catch. If the main method catch also has a throw new Exception() code then it logs the exception. However it will be logged with the stack trace pointing to MainMethod and not to the SecondMethod.

What I wanted what was that every time it reaches a catch without rethrowing a new Exception, Elmah would log the exception. This is because I have many try-catch blocks in my application and I want to log these exceptions without manually logging on each try-catch. However if you tell me how can I log the exception from SecondMethod that would be fine.

解决方案

Are you using ASP MVC?

The filters will only execute for unhandled exceptions thrown from the controller methods. (The context.ExceptionHandled property tells you if it has been handled by another filter, not in a try-catch block). So if you swallow the exceptions in try-catch blocks inside your methods then they will not be handled by the error filters.

You need to decide when you want to manually handle the exceptions inside your code using try-catch blocks (and in that case manually log the exceptions with the aid of a base controller class or a helper class) or let the exception bubble and be handled by your filters. (You probably will want a mixture of the two, depending on each particular use case)

When you decide to rethrow the exceptions, take a look at this SO question. Basically you can rethrow an exception preserving the stack trace with:

try
{
    //code
}
catch (Exception ex)
{
    //...some error handling code here... 
    //Otherwise why the try-catch at all?
    throw;
}

You could do that in your sample MainMethod and the exception logged would preserve the stack trace.

这篇关于使用Elmah在try..catch中处理日志异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆