using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Diagnostics;
namespace LinqToTwitterTests.Common
{
///
/// Implements a for writing information to the debugger log.
///
///
/// This code is created by Kris Vandermotten
/// /// For more information check this link: http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11
public class DebuggerWriter : TextWriter
{
private bool isOpen;
private static UnicodeEncoding encoding;
private readonly int level;
private readonly string category;
///
/// Initializes a new instance of the class.
///
public DebuggerWriter()
: this(0, Debugger.DefaultCategory)
{
}
///
/// Initializes a new instance of the class with the specified level and category.
///
/// A description of the importance of the messages.
/// The category of the messages.
public DebuggerWriter(int level, string category)
: this(level, category, CultureInfo.CurrentCulture)
{
}
///
/// Initializes a new instance of the class with the specified level, category and format provider.
///
/// A description of the importance of the messages.
/// The category of the messages.
/// An object that controls formatting.
public DebuggerWriter(int level, string category, IFormatProvider formatProvider)
: base(formatProvider)
{
this.level = level;
this.category = category;
this.isOpen = true;
}
protected override void Dispose(bool disposing)
{
isOpen = false;
base.Dispose(disposing);
}
public override void Write(char value)
{
if (!isOpen)
{
throw new ObjectDisposedException(null);
}
Debugger.Log(level, category, value.ToString());
}
public override void Write(string value)
{
if (!isOpen)
{
throw new ObjectDisposedException(null);
}
if (value != null)
{
Debugger.Log(level, category, value);
}
}
public override void Write(char[] buffer, int index, int count)
{
if (!isOpen)
{
throw new 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, new string(buffer, index, count));
}
public override Encoding Encoding
{
get
{
if (encoding == null)
{
encoding = new UnicodeEncoding(false, false);
}
return encoding;
}
}
public int Level
{
get { return level; }
}
public string Category
{
get { return category; }
}
}
}