是否可以(错误)使用 Exception.HelpLink 来识别 Exception 对象?
我正在开发一个日志程序,并且我希望避免在重复记录时重复处理相同的 Exception 对象,因为它是通过嵌套调用结构渗透的。因此,我希望能够格式化 Exception 对象一次,并为格式化版本提供唯一的“异常编号”,然后以某种方式标记 Exception 对象,以便我可以如果它在以后的日志调用中再次出现,请识别它。
我提出的想法是滥用 Exception
对象的 HelpLink
字段。我将其设置为包含“异常编号”的字符串版本。然后,如果 Exception
对象在另一个日志调用中再次出现,我就可以识别它。
但这可能是一个坏主意吗?有没有我没有想到的问题?如果是这样,有人有更好的主意吗?
编辑: 为了更详细地解释一下情况,这个记录器只会在我自己的程序上使用。
I'm working on a logging program, and I'd like to avoid processing the same Exception
object repeatedly when it is being logged repeatedly because it is percolating up through a nested call structure. So I'd like to be able to format the Exception
object once, and give the formatted version a unique "exception number" and then tag the Exception
object somehow so I can recognize it if it turns up again in a later log call.
The idea I've come up with is to misuse the HelpLink
field of the Exception
object. I'll set it to contain a string version of my "exception number". Then I can recognize the Exception
object if it shows up again momentarily in another log call.
But is this maybe a bad idea? Are there any gotchas involved that I haven't thought of? If so, does anyone have a better idea?
EDIT:
To explain the situation a bit more, this logger will only be used on my own programs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Data
属性,向Exception
添加额外信息。它包含提供有关异常的附加用户定义信息的键/值对。Instead of 'abusing'
HelpLink
property, you could useData
property to add extra information to theException
. It contains key/value pairs that provide additional user-defined information about the exception.虽然我同意 TheVillageIdiot 的观点,但我会指出,更一般地说,如果您想更改 Exception 的行为,那么您应该创建自己的 Exception 类来添加其他相关信息。毕竟,这就是我们使用继承和多态性的原因。 :)
While I agree with TheVillageIdiot, I would point out that more generally speaking, if you want to change the behavior of Exception, then you should create your own Exception class that add's additional pertinent information. That's why we use inheritance and polymorphism, after all. :)
绝对不能使用 Exception.HelpLink ,因为记录器应该只关心以给定格式或任何默认格式记录异常信息。如果相同的异常一次又一次出现,则是执行程序集或程序的问题,而不是记录器的问题。
更好的是,您可以探索使用 log4net 进行日志记录和自定义报告界面的选项,以对 log4.net 创建/更新的日志文件或数据库表中的异常进行格式化/分组
Definitely it is not okay to use
Exception.HelpLink
because logger should be concerned with logging the exception information only in given format or any default format. If same exception is coming again and again it is problem of the executing assembly or program not the logger.Better still you can explore the options of using
log4net
for logging and custom reporting interface to format/group exception from the log files or database tables created/updated by log4.net不,滥用帮助链接是不可接受的。正如 @Greebo 提到的,如果您需要其他属性,您可以创建自己的异常类。另一种方法可能是使用属于
System.Exception
类的Data
属性。问题:除了日志记录之外,您的异常处理程序是否还执行其他处理?
如果没有,那么您很可能不需要处理程序。只需让异常(使用finally块进行清理)在调用堆栈中冒泡并在最外层进行处理即可。如果您的处理程序正在处理异常,那么我不确定为什么您会在堆栈中进一步出现相同的异常。我认为您更有可能创建一个新的异常,将内部异常设置为已处理的异常。
No it is not acceptable to misuse the HelpLink. As @Greebo mentioned if you need additional properties you could create your own exception classes. An alternative might be to use the
Data
property that is part of theSystem.Exception
class.Question: Are your exception handlers doing any handling other than logging?
If not then most likely your don't need the handlers. Just let the exception (using a finally block for cleanup) bubble up the call stack and handle it at the outmost layer. If your handlers are handling the exception then I'm not sure why you would have the same exception further up the stack. I would think it would be more likely that you would create a new exception setting the inner exception to the one that was handled.
您的代码不应在每个级别捕获并记录异常。您的代码没有理由两次看到相同的异常。这听起来很像您正在使用“捕获每个异常”,这是一个主要的反模式。
Your code should not be catching and logging the exception at every level. There's no reason that your code should ever be seeing the same exception twice. This sounds very much like you are using "catch every exception", which is a major anti-pattern.