Trace custom objects

  • Thread starter Thread starter Donald Xie
  • Start date Start date
D

Donald Xie

Hi,

I'm implementing a custom trace listener to manage the trace of custom
objects. For instance, I want the custom trace listener to listen to only
instances of MyClass:

Trace.Write(instanceOfMyClass)

In the trace listener, I override (among others) Write(object o) and
Write(string message). I also implement Write(MyClass o), hoping
polymorphism will work here. However, it appears that Write(string) is
always called. The other overloaded versions that accept objects are never
called.

The documentation states that Trace.Write(object o) and
TraceListener.Write(object o) write the ToString() method of the object,
which explains the above behaviors.

Are there anything I can do to invoke the overloaded versions accepting
objects? I guess that making MyClass serializable, deserializing it when
writes to trace, and serializing it in my listener would probably work.
However, I want to be sure that this is really necessary because there are
no other better workarounds.

Thanks for any help.
Donald Xie
 
Sorry my bad, my test harness somehow didn't implement Write(object o). It
still wouldn't call my Write(MyClass), but I can live with that.

Thanks again for your time.
Donald Xie
 
Back
Top