何时记录异常? [英] When to log exception?

查看:165
本文介绍了何时记录异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
{
   // Code
}
catch (Exception ex)
{
   Logger.Log("Message", ex);
   throw;
}

在图书馆的情况下,我应该甚至记录异常吗?我应该抛出它并允许应用程序登录吗?我的担心是,如果我在库中记录异常,将有很多重复(因为库层将记录它,应用程序层将记录它,并且之间的任何东西),但如果我不记录在图书馆,很难跟踪错误。有没有最佳实践?

In the case of a library, should I even log the exception? Should I just throw it and allow the application to log it? My concern is that if I log the exception in the library, there will be many duplicates (because the library layer will log it, the application layer will log it, and anything in between), but if I don't log it in the library, it'll be hard to track down bugs. Is there a best practices for this?

推荐答案

我不会记录我不会做任何事情的异常 - 即,如果它只是通过一个例外处理程序。正如你已经提到的,这增加了很多不一定有帮助的噪音。更好地记录在您实际做某事的地方,或者在图书馆的情况下,转换到用户代码的边界。

I would not log an exception that I wasn't going to do anything with - ie, if it's just passing through an exception handler as in your example. As you already mentioned, this adds a lot of noise that isn't necessarily helpful. Better to log it at the point where you are actually doing something about it, or, in the case of a library, at the boundary where it transitions into the user's code.

那就是说,我总是尝试登录我正在抛出异常的点,触发异常的条件。这对于定义异常的原因来说更为有用;再加上,如果你遇到的情况足够糟糕,以免发生异常,我会说它也要求处理器花时间注销为什么。

That said, I always try to log at the point where I am throwing the exception and the condition that triggered the exception. This is much more useful to pin down the reason for the exception; plus, if the condition that you encounter is bad enough to warrant throwing an exception, I would say it also warrants spending the processor time logging out the 'why'.

这篇关于何时记录异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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