How to override Debug.Writeline?

  • Thread starter Thread starter Billy
  • Start date Start date
B

Billy

Hi!

How can I override "Debug.Writeline("Message")" so that will not show
that msg in Debug window, but wrote it to a file. I already have a code
to write to a file but I would like to use just "Debug" class (same
name) for that. Inside of that code I will decide if I will wrote that
message to debug window or to a file. I would like to use that two
options:
-Debug.Writeline("Message", 1) '1 is integer argument from 0-9
-Debug.Writeline("Message")

I would like to put that code in regular modul or class so I can call
then that method from any form or general procedure in application.
Im pretty sure that this can be done but don't know how. Can you help
me with that?

Billy
 
Hi Billy,

you never can override the Debug class (it's sealed).

Only add a TextWriterTraceListener to the Debug.Listeners.

Roland
 
So that mean that I cannot use the same command which will do/show
something else. For example:
I wrote Debug.Writeline("MessageX") --> and I got for example in debug
window or in a file "You wrote MessageX"

??
 
Billy,
You can use Debug.WriteLine, however you need to modify the Debug.Listeners
collection to do it.

You can modify the Debug.Listeners collection either in code or via your
app.config file.

I normally use the app.config, for example, try the following in your
app.config:

<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="0">
<listeners>
<remove name="Default" />
<add name="MyListener"
type="System.Diagnostics.TextWriterTraceListener, Version,
Culture, PublicKeyToken"
initializeData="MyListener.log"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>

Which should remove the default TraceListener (the one going to the Debug
Window) and adds a new TraceListener that writes to the MyListener.log file.


The following provide a good introduction to TraceListeners:

http://msdn.microsoft.com/msdnmag/issues/01/02/bugslayer/default.aspx

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconTraceListeners.asp

http://msdn.microsoft.com/library/d...en-us/cpgenref/html/gngrflistenerselement.asp

http://msdn.microsoft.com/library/d...f/html/gngrfaddelementforlistenerselement.asp

http://msdn.microsoft.com/library/d...ry/en-us/cpgenref/html/gngrfremoveelement.asp

http://msdn.microsoft.com/library/d...gnosticsTraceListenerCollectionClassTopic.asp

Hope this helps
Jay
 
Back
Top