This doesn't count for a library, but I wrote a Text box listener once so that I could just drop the text box on a form, and automagically get trace output....
public class TraceLogTextBox : RichTextBox
{
private TraceListener listener;
public TraceLogTextBox()
: base()
{
listener = new TextBoxTraceListener(this);
Trace.Listeners.Add(listener);
}
protected override void Dispose(bool disposing)
{
Trace.Listeners.Remove(listener);
base.Dispose(disposing);
}
}
public class TextBoxTraceListener : TraceListener
{
private RichTextBox txtBox;
const string ERROR = "error";
public TextBoxTraceListener(RichTextBox tbox)
: base()
{
txtBox = tbox;
}
public override void Write(object o)
{
if (o is Exception)
{
Write(o.ToString(), ERROR);
}
else
{
Write(o.ToString());
}
}
public override void Write(string message)
{
Write(message, string.Empty);
}
public override void Write(string message, string category)
{
Color col = category.ToLower() == ERROR ? Color.Red : Color.Black;
txtBox.SelectionColor = col;
if (category == string.Empty)
{
txtBox.SelectedText = message;
}
else
{
txtBox.SelectedText = string.Format("[{0}] {1}", category, message);
}
}
public override void WriteLine(string message)
{
WriteLine(message, string.Empty);
}
public override void WriteLine(object o)
{
if (o is Exception)
{
WriteLine(o.ToString(), ERROR);
}
else
{
WriteLine(o.ToString());
}
}
public override void WriteLine(string message, string category)
{
Color col = category.ToLower() == ERROR ? Color.Red : Color.Black;
txtBox.SelectionColor = col;
if (category == string.Empty)
{
txtBox.SelectedText = string.Format("{0}\n",message);
}
else
{
txtBox.SelectedText = string.Format("[{0}] {1}\n", category, message);
}
}
}