using System; using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; namespace Vandermotten.Diagnostics { /**////<summary> /// Implements a <see cref="TextWriter"/> for writing information to the debugger log. ///</summary> ///<seealso cref="Debugger.Log"/> publicclass DebuggerWriter : TextWriter { privatebool isOpen; privatestatic UnicodeEncoding encoding; privatereadonlyint level; privatereadonlystring category; /**////<summary> /// Initializes a new instance of the <see cref="DebuggerWriter"/> class. ///</summary> public DebuggerWriter() : this(0, Debugger.DefaultCategory) { } /**////<summary> /// Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level and category. ///</summary> ///<param name="level">A description of the importance of the messages.</param> ///<param name="category">The category of the messages.</param> public DebuggerWriter(int level, string category) : this(level, category, CultureInfo.CurrentCulture) { } /**////<summary> /// Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level, category and format provider. ///</summary> ///<param name="level">A description of the importance of the messages.</param> ///<param name="category">The category of the messages.</param> ///<param name="formatProvider">An <see cref="IFormatProvider"/> object that controls formatting.</param> public DebuggerWriter(int level, string category, IFormatProvider formatProvider) : base(formatProvider) { this.level = level; this.category = category; this.isOpen =true; } protectedoverridevoid Dispose(bool disposing) { isOpen =false; base.Dispose(disposing); } publicoverridevoid Write(char value) { if (!isOpen) { thrownew ObjectDisposedException(null); } Debugger.Log(level, category, value.ToString()); } publicoverridevoid Write(string value) { if (!isOpen) { thrownew ObjectDisposedException(null); } if (value !=null) { Debugger.Log(level, category, value); } } publicoverridevoid Write(char[] buffer, int index, int count) { if (!isOpen) { thrownew ObjectDisposedException(null); } if (buffer ==null|| index <0|| count <0|| buffer.Length - index < count) { base.Write(buffer, index, count); // delegate throw exception to base class } Debugger.Log(level, category, newstring(buffer, index, count)); } publicoverride Encoding Encoding { get { if (encoding ==null) { encoding =new UnicodeEncoding(false, false); } return encoding; } } publicint Level { get{ return level; } } publicstring Category { get{ return category; } } } }