For my WPF application I do logging to a text file using a TextWriterTraceListener. How can I also display the Trace output to a textbox?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
I use this for C# winforms, should be easily adjustable to wpf
Use it like
Remember to Trace/Debug.Listeners.Remove(debugListener); when you don't need it anymore.
Below code is C#6.0 style of @Mark Seemann's code.
Assume that MainViewModel is root
DataContext
of MainWindow.xaml file. To applyMyTraceListener
in MVVM manner, write below code in MainViewModel.cs.In MainWindow.xaml, bind
TraceOutput
property to TextBox. If you want the TextBox to scroll to bottom along with accumulated logs, applyTextChanged
event.in code-behind of XAML file (MainWindow.xaml.cs), event handler is simply as below.
you could append a custom Listener which updates the Textbox.Text property. You therefore need to inherit from the abstract base class TraceListener and override one of the TraaceData, TraceEvent, TraceTransfer methods.
How about implementing a custom TraceListener that simply appends trace messages to a string? You then expose that string as a property, implement INotifyPropertyChanged and databind a TextBox control to that property.
Something like this:
You would need to add this TraceListener to the list of active listeners: