On Fri, 17 Oct 2008 14:21:52 -0700, Ralf Jansen
<ra****************@deepinvent.comwrote:
[...]
My LoggingService.Trace methods return an IDisposable object which does
the timing. It starts timing in its constructor and logs the results in
its Dispose method so i need to know if an exception happened in the
Dispose method of that object.
What about this:
using (LoggingService.TraceObject to =
LoggingService.Trace("ActivityID"))
{
// do whatever...
to.ExceptionOccurred = false;
}
TraceObject could be the actual class (or base class) for the object
implementing IDisposable. Or, if for some reason an inheritance isn't
appropriate, it could be a simple one-property interface (call it
IExceptionState or something like that). The ExceptionOccurred property
could be write-only, basically providing input to the object implementing
IDisposable that you got all the way through the code block without an
exception.
Another alternative that doesn't rely on IDisposable:
LoggingService.Trace("ActivityID", delegate
{
// do whatever...
});
Where the LoggingService.Trace() method looks something like this:
void Trace(string strID, Action action)
{
bool fException = true;
try
{
action();
fException = false;
}
finally
{
// do your logging, taking fException into account
}
}
Ultimately, you're using IDisposable for something other than the purpose
for which it was intended. If you insist on using IDisposable for this
purpose, you may have to accept that this use isn't going to fit perfectly
into the code, unless you are willing for your logging object to have some
complicated, ugly hacks in it.
Personally, I'd go with an approach that didn't use IDisposable.
Pete