This is a Log implementation that will write messages out to any TextWriter. This class is NOT thread safe. You can make it thread safe in two ways.
  1. Use LockingLog decorator
  2. Use TextWriter.Synchronized

TextWriter ownership

When creating a TextWriterLog, you can specify the level of responsibility the instance should take for the TextWriter. It can be completely hands off, or it can own the TextWriter and close it on disposal.

Note on Disposal

Whether or not you call Dispose on the TextWriterLog depends on where the TextWriter writes to and when it can no longer be used. If you are creating a Log for a Logger that will exist the entire life of the AppDomain, than disposal probably isn't necessary. For file based TextWriters when the AppDomain shuts down, the writers are closed. You'll need to look at your TextWriter and decide whether or not disposal is needed.


TextWriterLog log = new TextWriterLog(Console.Out); // implies a hands off ownership level
TextWriterLog log = new TextWriterLog(new StreamWriter("somefile"), TextWriterResponsibility.Owns); // log owns the writer

StringWriter globalBuffer = new StringWriter();
TextWriterLog log = new TextWriterLog(TextWriter.Synchronized(globalBuffer), TextWriterResponsibility.DoesNotOwn);

Last edited Feb 12, 2009 at 7:46 PM by justrudd, version 2


No comments yet.