Log exceptions handled in try..catch with Elmah

不羁的心 提交于 2019-12-01 12:29:25
Daniel J.G.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!