我应该抓住唯一的例外日志呢? [英] Should I catch exceptions only to log them?
问题描述
我应该抓住进行日志记录异常?
Should I catch exceptions for logging purposes?
public foo(..)
{
try
{
...
} catch (Exception ex) {
Logger.Error(ex);
throw;
}
}
如果我在我的每个层(数据访问,商务和WebService)都有这种到位这意味着记录异常几次。
If I have this in place in each of my layers (DataAccess, Business and WebService) it means the exception is logged several times.
是否有意义这样做,如果我的层在不同的项目,只有公共接口具有的try / catch在其中?
为什么呢?为什么不?是否有不同的方法,我可以使用?
Does it make sense to do so if my layers are in separate projects and only the public interfaces have try/catch in them? Why? Why not? Is there a different approach I could use?
推荐答案
非也。你应该找到正确的位置为处理除外(实际上做一些事情,比如接球未重新抛出),然后记录它。您可以并且应该包括课程的整个堆栈跟踪,但按照你的建议将垃圾用的try-catch块中的代码。
Definitely not. You should find the correct place to handle the exception (actually do something, like catch-and-not-rethrow), and then log it. You can and should include the entire stack trace of course, but following your suggestion would litter the code with try-catch blocks.
这篇关于我应该抓住唯一的例外日志呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!