1

At the moment I write a lot of status messages out using the following:

Debug.Writeline("This is my status message");

I would like to be able to create a method that allows me to either output messages via Debug or redirect them to a textbox in my UI. However, the Debug and Textbox objects are not compatible. How can I do this in such a way that the caller can use the same message to generate output and let the calling method decide where to send it (i.e., to Debug, the UI, or even a log file)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • Use `Trace`, or something like log4net. – SLaks Aug 15 '13 at 16:25
  • If you're averse to third-party logging libraries, here's a suggested way to create a [generic Logger extension method](http://stackoverflow.com/questions/15377858/suggest-design-almost-every-object-in-app-has-loggger) – Sameer Singh Aug 15 '13 at 16:28

2 Answers2

1

You can create your own class derived from TraceListener, as described in the accepted answer to Trace listener to write to a text box (WPF application).

Note: The accepted answer details how to create the custom trace listener for C# WinForms, despite the question being about WPF.

Community
  • 1
  • 1
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

Create your own logging class that bridge's your debugging class

interface ILog {
   public void Log();
}

public DebugLog : ILog {
  public DebugLog(Debug d) {} //ctor that takes Debug Object
  public void Log(){}
}

public WindowLog : ILog {
  public WindowLog(Text t) {} //ctor that takes window log
  public void Log(){}
}
Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • I'd make those methods static, just so I don't have to remember to new up the `Log` class every time. – Sameer Singh Aug 15 '13 at 16:26
  • Is there a way to do this polymorphically, so I can pass in the Textbox.Text member or Debug object and somehow use the same method with both? I'm not sure if I am explaining myself well enough since I woke up way too early today and I am NOT a morning person. :) – gonzobrains Aug 15 '13 at 16:30