Is it OK to (mis)use Exception.HelpLink to recognize Exception objects?
Asked Answered
C

5

5

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.

Connubial answered 11/6, 2011 at 2:19 Comment(1)
I think asking the question in itself is a clear indicator of what the answer is.Fontanel
S
9

Instead of 'abusing' HelpLink property, you could use Data property to add extra information to the Exception. It contains key/value pairs that provide additional user-defined information about the exception.

Stoeber answered 11/6, 2011 at 2:33 Comment(3)
I had to LOL at the idea of abusing HelpLink. I had a sudden vision of the HelpLink Abuse Hotline getting calls in the middle of the night...Graveyard
Thanks, that's a much better idea than getting involved in an activity that could result in middle-of-the-night calls to a hotline.Connubial
Could be even worse - I mean you don't want to end up on the national List of Registered Property Abusers ;)Graveyard
G
4

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. :)

Graveyard answered 11/6, 2011 at 2:30 Comment(2)
very valid suggestion but again logger will need to know about the custom exception classes. He can use application specific layer above some general logging mechanism.Bathsheeb
Absolutely - and as you said, the problem here is in the structure. Logging of exceptions should never happen except at the top most layer to avoid these redundant recordings.Graveyard
B
2

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

Bathsheeb answered 11/6, 2011 at 2:26 Comment(1)
Thanks for your answer. Please see the edit I added to my question. I can also mention that I currently use log4net, but am switching to a home-made logger. Also, the situation were an exception is logged several times as it "percolates" up through a nested call structure is normal for the way I write exception handling, for what it's worth ...Connubial
G
0

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 the System.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.

Grados answered 11/6, 2011 at 2:33 Comment(0)
O
0

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.

Objection answered 13/6, 2011 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.